Python CSV Exercises: Read, Write, and Quote
CSV looks like plain text until a comma appears inside a field. The csv module handles quoting, delimiters, and row boundaries so you do not have to split lines by hand.
Read rows as lists
csv.reader returns an iterator of lists. Every field starts as a string, including numbers. The first row is not special unless you decide it is the header. Materialize with list when you need to inspect several rows.
import csv
from io import StringIO
rows = list(csv.reader(StringIO("a,b\n1,2\n")))
print(rows[0])
print(rows[1][1])Rows are lists of strings
Use the header as keys
DictReader treats the first row as field names. Each later row becomes a dictionary, which makes column access readable. If the file has no header, pass fieldnames explicitly so the first data row is not consumed as labels.
import csv
from io import StringIO
raw = "name,score\nAda,92\n"
rows = list(csv.DictReader(StringIO(raw)))
print(rows[0]["name"])Named columns
Write one row at a time
csv.writer writes a row and the required line terminator. Pass a list of fields, not a pre-joined string, so the module can quote correctly. Use writerows only when you already have an iterable of rows.
import csv
from io import StringIO
buffer = StringIO()
writer = csv.writer(buffer)
writer.writerow(["a", "b"])
print(buffer.getvalue().strip())Let the writer format the row
Quote when the data demands it
QUOTE_MINIMAL is the default and quotes only when needed. QUOTE_ALL quotes every field. A field containing a comma, quote, or newline must be quoted, and the csv module handles the escaping for you.
import csv
from io import StringIO
buffer = StringIO()
writer = csv.writer(buffer, quoting=csv.QUOTE_ALL)
writer.writerow(["a,b", "c"])
print(buffer.getvalue().strip())Quoting is structural
Open files with newline=""
When reading or writing a real file, open it with newline="" and let csv control line endings. This avoids blank lines on some platforms. Specify encoding explicitly, especially when the data contains non-ASCII text.
import csv
from io import StringIO
buffer = StringIO(newline="")
writer = csv.writer(buffer)
writer.writerow(["name", "note"])
print(buffer.getvalue().strip())Leave newline handling to csv
Your practice checklist
- Remember that csv.reader returns strings, not numbers.
- Use DictReader when the header gives useful column names.
- Pass field lists to writer.writerow instead of joined strings.
- Use QUOTE_ALL only when every field should be quoted.
- Open real CSV files with newline="" and an explicit encoding.
Common questions
Why does DictReader return strings for numbers?
CSV has no type information. Convert fields explicitly with int or float after validating them.
Do I need pandas to read CSV?
No. The standard csv module is enough for straightforward reads and writes. pandas is useful when you need tabular analysis.
What does QUOTE_MINIMAL do?
It quotes a field only when the field contains a delimiter, quote character, or line break. It is the default quoting mode.
Practice CSV
12 questions on reader, DictReader, writer rows, quoting, and file handling.
Keep reading: