liveBook · Manning

Csv

As explained in The Quick Python Book, Fourth Edition

CSV is a Python library for reading and writing tabular data in comma-separated and other delimited formats.

Python’s standard-library `csv` module reads and writes CSV and other delimited text files. Use it instead of splitting lines by hand: quoted fields can contain delimiters, and the module handles those fields when reading or writing. section 21.2.4 section 21.5.1

Choose an interface to match the records:

  • `csv.reader(file, delimiter="|")` reads pipe-delimited records as lists of fields; omit `delimiter` for comma-delimited input. section 21.2.4
  • `csv.writer(file).writerows(rows)` writes a sequence of rows. Open the output with `newline=''` as shown in the file-writing example. section 21.5.1
  • `csv.DictWriter(file, fieldnames=fields)` writes dictionary rows in the specified field order; call `writeheader()` to add column names. section 21.5.1

Read a quoted field, then write the row

`rows = list(csv.reader(['Illinois,"Jan 01, 1979",17.48']))` produces `[['Illinois', 'Jan 01, 1979', '17.48']]`: the comma inside the quoted date does not start another field. To save the rows, open a destination file with `newline=''` and pass them to `csv.writer(destination).writerows(rows)`. section 21.2.4 section 21.5.1

results2 = [fields for fields in csv.reader(open("temp_data_01.csv", newline=''))]
results2
 
[['Notes', 'State', 'State Code', 'Month Day, Year', 'Month Day, Year
Code', 'Avg Daily Max Air Temperature (F)', 'Record Count for Daily Max Air
Temp (F)', 'Min Temp for Daily Max Air Temp (F)', 'Max Temp for Daily Max
Air Temp (F)', 'Avg Daily Max Heat Index (F)', 'Record Count for Daily Max
Heat Index (F)', 'Min for Daily Max Heat Index (F)', 'Max for Daily Max
Heat Index (F)', 'Daily Max Heat Index (F) % Coverage'], [], ['',
'Illinois', '17', 'Jan 01, 1979', '1979/01/01', '17.48', '994', '6.00',
'30.50', 'Missing', '0', 'Missing', 'Missing', '0.00%'], ['', 'Illinois',
'17', 'Jan 02, 1979', '1979/01/02', '4.64', '994', '-6.40', '15.80',
'Missing', '0', 'Missing', 'Missing', '0.00%'], ['', 'Illinois', '17', 'Jan
03, 1979', '1979/01/03', '11.05', '994', '-0.70', '24.70', 'Missing', '0',
'Missing', 'Missing', '0.00%'], ['', 'Illinois', '17', 'Jan 04, 1979',
'1979/01/04', '9.51', '994', '0.20', '27.60', 'Missing', '0', 'Missing',
'Missing', '0.00%'], ['', 'Illinois', '17', 'May 15, 1979', '1979/05/15',
'68.42', '994', '61.00', '75.10', 'Missing', '0', 'Missing', 'Missing',
'0.00%'], ['', 'Illinois', '17', 'May 16, 1979', '1979/05/16', '70.29',
'994', '63.40', '73.50', 'Missing', '0', 'Missing', 'Missing', '0.00%'],
['', 'Illinois', '17', 'May 17, 1979', '1979/05/17', '75.34', '994',
'64.00', '80.50', '82.60', '2', '82.40', '82.80', '0.20%'], ['',
'Illinois', '17', 'May 18, 1979', '1979/05/18', '79.13', '994', '75.50',
'82.10', '81.42', '349', '80.20', '83.40', '35.11%'], ['', 'Illinois',
'17', 'May 19, 1979', '1979/05/19', '74.94', '994', '66.90', '83.10',
\'82.87', '78', '81.60', '85.20', '7.85%']]

The parsed temperature data shows quoted, comma-containing fields kept intact. It also shows an empty list for a blank input row, which downstream processing may need to handle. section 21.2.4

Parsing is only one step in a data-cleaning job. For the Illinois weather data, filter the desired county, stop before the trailing documentation, replace `Missing`, convert percentages, and then write the selected records. Check field positions carefully: searching an entire row for `Cook` is not the same as checking the county field. section 21.6 section 21.6.2

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Works through readers, writers, and dictionary output with file examples, without examining module internals.
Examples
Uses pipe-delimited temperature rows, quoted date fields, and an Illinois weather extraction exercise.
What it assumes
Assumes Python file iteration, lists, and dictionaries while introducing csv operations through comparisons with hand-written parsing.
What you can do after
Apply it via Python’s csv.reader, csv.writer, and csv.DictWriter to parse and export delimited records.
How it connects
Links delimited files to extract-transform-load, data cleaning, and spreadsheet output.

Also explained in

Compare how 4 books explain csv →

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.