Reading and writing CSV files with Python
csv module, csv.reader, csv.writer, csv.DictReader, csv.DictWriter, newline parameter, delimiter, quoting
CSV Files in Python
The csv module handles delimiter-separated files. Always open CSV files with newline="" to let the csv module handle line endings correctly across platforms.
import csv
# Writing
with open("users.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerow(["name", "age", "city"]) # header
writer.writerows([
["Alice", 30, "NY"],
["Bob", 25, "LA"]
])
# Reading with DictReader (most convenient)
with open("users.csv", "r", newline="", encoding="utf-8") as f:
reader = csv.DictReader(f)
for row in reader:
print(row["name"], row["age"]) # Alice 30, Bob 25
DictWriter
fields = ["name", "age", "city"]
with open("out.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=fields)
writer.writeheader()
writer.writerow({"name": "Eve", "age": 28, "city": "SF"})
DictReader and DictWriter are preferred over the plain reader/writer for anything with more than two columns — they make column access by name explicit and immune to column reordering bugs.
CSV is deceptively simple — real-world CSV files often have inconsistent quoting, embedded commas, BOM markers, or mixed line endings that trip up naive parsers. The csv module handles all of these correctly. For large CSV files (hundreds of thousands of rows), iterate row by row with a for loop rather than readlines() to avoid loading the entire file into memory. When dealing with numeric data, remember that csv reads everything as strings — convert values explicitly. The pandas library is the standard tool for heavy CSV analysis, but the csv module is sufficient for simple reads and writes.
