Process CSV Rows Without Loading Everything
Stream CSV rows with the csv module, validate required headers, convert typed fields, and route rejected rows to a separate file.
Code
import csv
import tempfile
from collections.abc import Iterator
from pathlib import Path
from typing import Any
REQUIRED = ("id", "email", "age")
def stream_rows(
source_path: Path,
rejects_path: Path,
) -> Iterator[dict[str, Any]]:
with (
source_path.open("r", encoding="utf-8", newline="") as source,
rejects_path.open("w", encoding="utf-8", newline="") as rejects,
):
reader = csv.DictReader(source)
fieldnames = reader.fieldnames or []
missing = [name for name in REQUIRED if name not in fieldnames]
if missing:
raise ValueError("missing columns: " + ", ".join(missing))
writer = csv.DictWriter(rejects, fieldnames=["line", "reason"])
writer.writeheader()
for line_no, row in enumerate(reader, start=2):
raw_age = (row.get("age") or "").strip()
try:
age = int(raw_age)
except ValueError:
writer.writerow({"line": line_no, "reason": "age is not an integer"})
continue
if not 18 <= age <= 120:
writer.writerow({"line": line_no, "reason": "age outside 18..120"})
continue
email = (row.get("email") or "").strip().lower()
if "@" not in email:
writer.writerow({"line": line_no, "reason": "email is invalid"})
continue
yield {
"id": (row.get("id") or "").strip(),
"email": email,
"age": age,
}
def main() -> None:
with tempfile.TemporaryDirectory() as tmp:
source = Path(tmp) / "people.csv"
rejects = Path(tmp) / "rejects.csv"
source.write_text(
"id,email,age\n"
"1,Ada@Example.com,36\n"
"2,grace@example.com,n/a\n"
"3,linus@example.com,17\n",
encoding="utf-8",
)
valid = list(stream_rows(source, rejects))
print(valid)
print(rejects.read_text(encoding="utf-8").strip())
if __name__ == "__main__":
main()
How it works
Process CSV Rows Without Loading Everything keeps everything in the functions stream_rows and main.
Iteration happens inside for line_no, row in enumerate(reader, start=2) — everything outside it runs once. The import line is the dependency contract: Iterator, Path, Any come from csv and nothing else. ValueError is the guard’s signal — thrown at the branch, before any partial work happens.
What to watch for
- Requires
csvandtempfileas packages; the snippet will not run without them. - The guard
missingshort-circuits the flow with an earlyraisewhen its condition holds.
Putting it to use
Entry signature: def stream_rows( source_path: Path, rejects_path: Path, ) -> Iterator[dict[str, Any]]
Launch the file; main takes over and steps through stream_rows.
Core lines
except ValueError:
writer.writerow({"line": line_no, "reason": "age is not an integer"})
continue
def stream_rows(
source_path: Path,
rejects_path: Path,
) -> Iterator[dict[str, Any]]:
Interactive Tools for This Topic
FAQ
- Is there a failure path for bad input?
- It throws `ValueError` before any real work runs, via the guard `missing` — handle it at the call site or let it propagate deliberately.
- How do you run Process CSV Rows Without Loading Everything?
- Run it as a complete program — compile or interpret the file directly; there is nothing to install first.
Report a problem with this exampleOpens a pre-filled issue on GitHub.