Python

Stream Large CSV with DictReader

admin by @admin ADMIN
1d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Iterate over a CSV row-by-row as a dict — never loading the whole file. Tolerant of UTF-8 BOMs (utf-8-sig). Generator wrapper makes consumers naturally use for/break.
Python
Raw
import csv
from pathlib import Path
from typing import Iterator

def csv_rows(path: str | Path) -> Iterator[dict[str, str]]:
    with Path(path).open("r", encoding="utf-8-sig", newline="") as f:
        yield from csv.DictReader(f)

for row in csv_rows("export.csv"):
    print(row["email"], row["signup_date"])
    # uses ~constant memory regardless of file size

# Count by category lazily — only one row in RAM at a time
from collections import Counter
counts = Counter(row["category"] for row in csv_rows("transactions.csv"))
print(counts.most_common(10))
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.