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
- CSV — Comma-separated values: a plain-text table format with one row per line and fields separated by commas.
- dictionary — A Python collection that stores values under keys, written in curly brackets, such as {"tea": 250}.
- split() — A string method that cuts a string into a list of parts, at white space by default or at a separator you give.
Taught in
- JSON And CSV — Hands-on Python: Complete Video Course & Book