PythonMastery

How do I read a CSV file in Python?

Open the file with newline='' and hand it to csv.DictReader: each row arrives as a dictionary keyed by the header. Every value is a string until you convert it.

Use the csv module from the standard library. DictReader reads the header line for you and gives back each row as a dictionary, so you write row["city"] instead of remembering that the city was column 2.

python
# Make a small file to read. Skip this part if you already have one.
with open("people.csv", "w", newline="", encoding="utf-8") as f:
    f.write("name,city,age\nAda,London,36\nLinus,Helsinki,28\nGrace,New York,45\n")

import csv

with open("people.csv", newline="", encoding="utf-8") as f:
    for row in csv.DictReader(f):
        print(row["name"], "lives in", row["city"])
output
Ada lives in London
Linus lives in Helsinki
Grace lives in New York

Every value is a string

The CSV format has no idea what a number is. "36" comes back as text, so + either raises an error or, worse, joins two strings instead of adding numbers.

python
import csv, io

data = io.StringIO("name,age\nAda,36\nLinus,28\n")   # behaves like an open file
ages = [int(row["age"]) for row in csv.DictReader(data)]
print(sum(ages) / len(ages))
output
32.0

Convert at the moment you read, once, and the rest of the program can trust the types.

With pandas, for anything you'll analyse

If the next thing you'll do is filter, group or average, read it straight into a DataFrame. pandas also works out the column types for you. (The first Run here takes a few seconds while pandas downloads.)

python
import io
import pandas as pd

data = io.StringIO("name,city,age\nAda,London,36\nLinus,Helsinki,28\nGrace,New York,45\n")
df = pd.read_csv(data)          # or pd.read_csv("people.csv")
print(df["age"].mean())
print(df[df["age"] > 30]["name"].tolist())
output
36.333333333333336
['Ada', 'Grace']

Two common problems

Always pass newline="" when you open a CSV. The csv module handles line endings itself. Without it, a line break inside a quoted field is not read exactly as it is in the file: a Windows \r\n comes back as \n.

Go deeper: CSV, JSON, and JSONL: Structured Text Done Right, Working With Your Own Data Files, UnicodeDecodeError: 'utf-8' codec can't decode byte

Full program: Read a CSV file in Python

More short answers

all questions