csv module

The standard library module for reading and writing CSV files correctly, including quoted fields that contain commas.

In more detail

csv.reader gives each row as a list of strings, and csv.DictReader gives each row as a dictionary keyed by the header line. Open CSV files with newline="" as the documentation asks, so line endings are handled correctly.

Example

import csv
with open("people.csv", newline="", encoding="utf-8") as f:
    for row in csv.DictReader(f):
        print(row["name"])

Related terms

Taught in

References