📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Python from Zero Working with CSV

Working with CSV

5 min read
Read CSVs with csv.DictReader to get each row as a dict. Write with csv.DictWriter. Open CSV files with newline='' to avoid double newlines on Windows. Use pandas for analysis on large CSV files.

CSV Files

import csv

# Read
with open("data.csv") as f:
    reader = csv.DictReader(f)
    for row in reader:
        print(row["name"], row["age"])

# Write
with open("out.csv", "w", newline="") as f:
    writer = csv.DictWriter(f, fieldnames=["name", "age"])
    writer.writeheader()
    writer.writerow({"name": "Alice", "age": 28})