CSV is the most common data exchange format, and splitting lines on commas breaks on real data.
A CSV file looks simple, but fields can contain commas, quotes and even newlines when quoted: "Smith, Jane","said ""hi"" twice". line.split(",") gets that wrong. csv.reader parses it correctly.
with open("people.csv", newline="", encoding="utf-8") as f:
for row in csv.reader(f):
print(row)Each row is a list of strings. Note the details:
- Open the file with
newline="". The csv module handles line endings itself; without it, newlines inside quoted fields can be mangled and\r\nendings misread. - Pass an explicit
encoding; the default depends on the operating system. - The reader is an iterator: it reads lazily, one row at a time, so files bigger than memory are fine. It can only be consumed once.
- The header row is just the first row. Skip it with
header = next(reader). reader.line_numtells you the current physical line number, useful in error messages.
Values are never converted: "42" stays a string. A blank line comes back as an empty list [], so guard with if not row: continue when files may contain them.
Example
import csv
with open("people.csv", "w", newline="", encoding="utf-8") as f:
f.write('name,city,quote\nAda,London,"said ""hi"" twice"\n"Smith, Jane",Paris,"two\nlines"\n')
with open("people.csv", newline="", encoding="utf-8") as f:
reader = csv.reader(f)
header = next(reader)
print(header) # ['name', 'city', 'quote']
for row in reader:
print(reader.line_num, row)
# 2 ['Ada', 'London', 'said "hi" twice']
# 4 ['Smith, Jane', 'Paris', 'two\nlines'] (one record, two physical lines)Remember
- Always open CSV files with newline='' and an explicit encoding
- csv.reader yields each row as a list of strings
- Quoted fields may contain commas, quotes and newlines
Watch out for
- Parsing CSV with line.split(','), which breaks on quoted commas and newlines.