Skip to content
GreenSnippets

Read a CSV file as dictionaries

Stream CSV rows as dicts keyed by header with csv.DictReader, handling UTF-8 BOMs and quoted commas.

Python
import csv


def read_csv(path):
    with open(path, newline="", encoding="utf-8-sig") as handle:
        yield from csv.DictReader(handle)

Example / expected output

Output
for row in read_csv("users.csv"):
    print(row["email"])

How it works

Open files with newline="" so the csv module handles embedded newlines itself, and utf-8-sig so a BOM from Excel does not become part of the first header name. The generator yields one row at a time, so large files do not fill memory.