liveBook · Manning

CSV

As explained in Tiny Python Projects: Learn coding and testing with puzzles and games

A CSV file is a delimited text file that represents tabular records using rows and separators between fields.

CSV is delimited text arranged as rows and fields. A comma separates values, the first line commonly names columns, and later lines represent records. section 19.0

A practical Python workflow treats CSV data as structured records:

  • Parse records manually with open(), rstrip(), split(), zip(), and dict(), or use csv.DictReader() for shorter, reusable code. section 19.1.2 section 19.1.3
  • Convert numeric-looking text explicitly, such as splitting a reps range at '-' and applying int() to both bounds. section 19.1.4 section 19.5
  • Use tests with in-memory input, then decide how to handle empty files, missing rows, changed headers, alternate delimiters, and malformed values. section 19.1.4 section 19.1.7
  • Format parsed records with tabulate, and inspect arbitrary delimited files with csvkit tools. section 19.1.1 section 19.1.8

Workout generator

Given exercise,reps rows such as Burpees,20-50, read each record, split the range, choose a random integer, select exercises, and print a text table. A reusable read_csv() function can return tuples such as ('Burpees', 20, 50). section 19.1.4 section 19.1.8

$ csvlook --max-rows 3 inputs/exercises.csv
| exercise | reps   |
| -------- | ------ |
| Burpees  | 20-50  |
| Situps   | 40-100 |
| Pushups  | 25-75  |
| ...      | ...    |

Inspecting a CSV as a table makes column names, row values, and delimiter-based structure immediately visible. section 19.1.1

For data analysis, pandas.read_csv() loads delimited data into a two-dimensional data frame, though its broader use is outside this treatment. section 19.3.3

How Tiny Python Projects: Learn coding and testing with puzzles and games covers it

How deep it goes
Explains manual parsing, DictReader, numeric conversion, testing, and malformed inputs, while deferring broader pandas use.
Examples
Uses exercise-and-repetition files, manual record lists, malformed input files, csvlook output, and a randomized text workout table.
What you can do after
Build it from scratch with Python’s csv module, test conversions, handle bad records, and produce formatted tabular output.
How it connects
Connects CSV with delimited text files, dictionaries, regular expressions, testing, tabulate, csvkit, and data frames.

Also explained in

Compare how 15 books explain CSV →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Tiny Python Projects: Learn coding and testing with puzzles and games. The section links point to the parts of the book it draws on.