Good place to stop if you're short on time — step 3 picks up here.
Lesson idea Tap to fold
What is the difference between file modes "w" and "a"?
Show the answer
Write replaces the file; append adds to its end.
Read CSV as labelled records
CSV can contain quoted commas, so splitting each line on a comma is not a general CSV parser. csv.DictReader uses the header to label each field. StringIO lets us practise with text without needing a file.
CSV values arrive as strings. Convert frames with int before numeric work. This introductory task assumes a valid header and numeric text; validation and malformed-row policies come later.
# Read CSV as labelled records.
import csv
from io import StringIO
data = 'frames,name\n0007,"Canal, East"\n'
for row in csv.DictReader(StringIO(data)):
frames = int(row["frames"])
print(row["name"], "·", frames, "frames")
Canal, East · 7 frames. The quoted comma stays inside the name, and 0007 becomes the integer 7.
Retrieve fields by their header keys, not their source position. Keep text and integers distinct even when they print similarly.
Predict, explain, then verify
These brief checks are self-reviewed, not quiz scores. Browser runs use fresh temporary files; they do not open your computer’s files. Download useful reports before another Run, Reset, exercise change or leaving.
Predict the type
Does CSV frame text 0007 arrive as the integer 7?
Compare your answer · self-reviewed
No. It arrives as text; int converts it to 7.
Read a header
If the CSV columns reorder, should dictionary-key lookups change?
Compare your answer · self-reviewed
No, provided the header names still match the data columns.
Spot a parser bug
Why is splitting a CSV row on commas unsafe for the name "Canal, East"?
Compare your answer · self-reviewed
The quoted comma is part of the name. A CSV parser understands quoting; a simple string split does not.
Choose an exercise to load its prompt.
Loading exercises…
Noted on your route map, with the step you were on. Nothing is lost by parking it — the next review day will bring this idea back, and the flag tells the course where to slow down.
The tutor mounts here when JavaScript is available. The lesson above stays readable without it.