In a structured text file, a row is one record: related field values kept together. A delimiter such as a comma, tab, or pipe separates the fields. Processing the file means reading records, selecting the ones needed, changing field values, and writing the results. section 21.0 section 21.6.2
With Python’s csv.reader, each row can be accessed by field index. Check the field that actually contains the value you want: `'Cook' in row` tests whether any whole field equals `Cook`, whereas `'Cook' in row[3]` finds it within a field such as `Cook County, IL`. The first test can silently leave an output file with only its header. section 21.6.2
A weather-file conversion shows the usual record-by-record steps:
- Read tab-delimited rows and stop when the first field marks the start of documentation. section 21.6.2
- Keep rows for Cook County, remove unwanted fields, replace `Missing` with an empty string, and convert percentages to decimals. section 21.6.2
- Write each processed row to a CSV file; preserve the header if the output needs one. section 21.6.2
import csv
with (open("Illinois_weather_1979-2011.txt") as infile,
open("Illinois_weather_1979-2011.csv", "w")
as outfile): #1
for row in csv.reader(infile, delimiter="\t"):
if row[0] == '---': #2
break
if "Cook" in row[3]:
del row[0:2] #3
row = [item.replace("Missing", "")
for item in row] #4
row[-1] = row[-1].replace("%", "") #5
row[-1] = (float(row[-1])/100 if str.isdigit(row[-1])
else row[-1]) #6
csv.writer(outfile).writerow(row)
A complete loop reads, filters, edits, and writes weather records. Notice that it tests `row[3]` before removing fields from the row. section 21.6.2
A record need not be a delimited text row. A class instance can hold named fields accessed with dot notation. For binary files, `struct` can pack a record’s values into bytes and unpack them back into a tuple; its format string specifies the field layout. section 15.1.1 section 13.7











