A CSV file is a list of rows separated by newlines, with values in each row separated by commas. The first line is usually the header — column names. Python's csv module knows the rules.
import csv
with open("data.csv") as f:
reader = csv.DictReader(f)
for row in reader:
print(row)What's row?
A dict — column name to cell value. If the CSV looks like:
key,value
a,10
b,20Then the loop iterates two rows: {"key": "a", "value": "10"} and {"key": "b", "value": "20"}. Note both values are strings — csv doesn't convert types. "10" not 10.
What's the difference between csv.reader and csv.DictReader?
csv.reader gives you a list per row (["a", "10"]). csv.DictReader reads the first line as headers and returns dicts. Use DictReader when columns have meaning and you'll reference cells by name. Use reader when columns are positional and the file has no header.
And it handles edge cases — quoted values with commas inside, escaped quotes?
Yes. "a,b",10 parses to one cell "a,b" and one cell "10". Don't roll your own CSV parser with .split(",") — the rules are subtle and the module handles them.
csv.DictReader — read CSV as a stream of dictsimport csv
with open("data.csv") as f:
reader = csv.DictReader(f)
for row in reader:
print(row["key"], row["value"])header → cell_value.int(row["value"]).File data.csv:
key,value
a,10
b,20
c,30Loop:
import csv
with open("data.csv") as f:
for row in csv.DictReader(f):
print(row)
# {'key': 'a', 'value': '10'}
# {'key': 'b', 'value': '20'}
# {'key': 'c', 'value': '30'}import csv
with open("data.csv") as f:
for row in csv.DictReader(f):
if row["key"] == "c":
print(row["value"])
break
# 30For TSV (tab-separated):
csv.DictReader(f, delimiter="\t")csv.DictWriter is the symmetric tool for writing rows from dicts. Same shape, write side. Lesson 17 sticks with JSON for now; you'll come back to DictWriter when you need it.
.split(",")?A cell value can contain commas if it's quoted: "hello, world",42 — that's one cell hello, world and a second cell 42. A naive split(",") produces three cells. The csv module follows RFC 4180 — quoting, escaping, line endings. Use it.
row["value"] is "10", not 10. Convert before arithmetic.DictReader, the first data row gets eaten as headers. Either fix the file or use csv.reader.open(..., newline="") is the recommended way on some platforms — Pyodide-on-the-web is forgiving here, but it's good practice.A CSV file is a list of rows separated by newlines, with values in each row separated by commas. The first line is usually the header — column names. Python's csv module knows the rules.
import csv
with open("data.csv") as f:
reader = csv.DictReader(f)
for row in reader:
print(row)What's row?
A dict — column name to cell value. If the CSV looks like:
key,value
a,10
b,20Then the loop iterates two rows: {"key": "a", "value": "10"} and {"key": "b", "value": "20"}. Note both values are strings — csv doesn't convert types. "10" not 10.
What's the difference between csv.reader and csv.DictReader?
csv.reader gives you a list per row (["a", "10"]). csv.DictReader reads the first line as headers and returns dicts. Use DictReader when columns have meaning and you'll reference cells by name. Use reader when columns are positional and the file has no header.
And it handles edge cases — quoted values with commas inside, escaped quotes?
Yes. "a,b",10 parses to one cell "a,b" and one cell "10". Don't roll your own CSV parser with .split(",") — the rules are subtle and the module handles them.
csv.DictReader — read CSV as a stream of dictsimport csv
with open("data.csv") as f:
reader = csv.DictReader(f)
for row in reader:
print(row["key"], row["value"])header → cell_value.int(row["value"]).File data.csv:
key,value
a,10
b,20
c,30Loop:
import csv
with open("data.csv") as f:
for row in csv.DictReader(f):
print(row)
# {'key': 'a', 'value': '10'}
# {'key': 'b', 'value': '20'}
# {'key': 'c', 'value': '30'}import csv
with open("data.csv") as f:
for row in csv.DictReader(f):
if row["key"] == "c":
print(row["value"])
break
# 30For TSV (tab-separated):
csv.DictReader(f, delimiter="\t")csv.DictWriter is the symmetric tool for writing rows from dicts. Same shape, write side. Lesson 17 sticks with JSON for now; you'll come back to DictWriter when you need it.
.split(",")?A cell value can contain commas if it's quoted: "hello, world",42 — that's one cell hello, world and a second cell 42. A naive split(",") produces three cells. The csv module follows RFC 4180 — quoting, escaping, line endings. Use it.
row["value"] is "10", not 10. Convert before arithmetic.DictReader, the first data row gets eaten as headers. Either fix the file or use csv.reader.open(..., newline="") is the recommended way on some platforms — Pyodide-on-the-web is forgiving here, but it's good practice.Create a free account to get started. Paid plans unlock all tracks.