liveBook · Manning

Record

As explained in The Quick Python Book, Fourth Edition

A row is a horizontally aligned record of related values in a tabular data structure.

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

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Defines rows as delimited records and works through field indexing and cleaning, without specifying a general record schema.
Examples
Uses Illinois weather rows to demonstrate county filtering, missing-value replacement, percentage conversion, and a field-selection bug.
What it assumes
Assumes familiarity with Python loops and indexing while introducing the idea that a delimited row represents a record.
What you can do after
Apply it via Python csv.reader and csv.writer to filter, clean, and write tabular records.
How it connects
Connects records to CSV processing, class instances, and binary struct formats.

Also explained in

Compare how 12 books explain record →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.