Skip to main content

How to Read a CSV File into Dictionaries in Python with csv.DictReader

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

csv.DictReader reads a CSV file row by row and gives you each row as a dictionary, keyed by the names in the header line. You write row["city"] instead of remembering that the city is the second column. This guide reads a small people.csv on Python 3.15.2.

Syntax​

Syntax
import csv

with open(<file name>, newline="", encoding="utf-8") as file:
for row in csv.DictReader(file):
<use row[<column name>]>
PartWhat it means
<file name>The CSV file to read, such as "people.csv"
newline=""Leaves the line endings to the csv module
csv.DictReader(file)Reads the first line as the column names, then gives one dictionary per line after it
row[<column name>]The value of that column in this row, always a string

The csv documentation says DictReader operates "like a regular reader but maps the information in each row to a dict", and that when you don't pass fieldnames, "the values in the first row of file f will be used as the fieldnames".

The Example​

A file with a header line and two people. The script reads it into a list of dictionaries, prints the list, prints one person's name and age, and shows the type of the age.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Create the CSV File​

Create people.csv next to your script:

people.csv
name,city,age
Ana,Lisbon,34
Ben,Leeds,28

Step 2: Write the Script​

read_people.py
import csv

with open("people.csv", newline="", encoding="utf-8") as file:
rows = list(csv.DictReader(file))

print(rows)
print(rows[0]["name"], "is", rows[0]["age"])
print(type(rows[0]["age"]))

The read_people.py script in a macOS-style code window: it opens people.csv, turns csv.DictReader into a list called rows, and prints the list, the first person's name and age, and the type of the age.

  • list(csv.DictReader(file)) reads every row while the file is open.
  • rows[0] is the first person, and rows[0]["name"] is the value in their name column.
  • type(...) shows what kind of value the age is.

Step 3: Run It​

Terminal
uv run python read_people.py
Output
[{'name': 'Ana', 'city': 'Lisbon', 'age': '34'}, {'name': 'Ben', 'city': 'Leeds', 'age': '28'}]
Ana is 34
<class 'str'>

The terminal after uv run python read_people.py: a list of two dictionaries with the keys name, city and age, then Ana is 34, then class str.

Each line became a dictionary whose keys are the header's names. Look at the age: '34' is in quotes, and its type is str. A CSV file holds only text, and DictReader doesn't guess types. The docs say it plainly: "No automatic data type conversion is performed". Turn a value into a number yourself with int(row["age"]) or float(...). The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Doing math on a value, such as rows[0]["age"] + 1TypeError: can only concatenate str (not "int") to strConvert it first: int(rows[0]["age"]) + 1 gives 35
Making the reader inside the with block and looping over it after the blockValueError: I/O operation on closed file.Loop inside the block, or make a list with list(...) inside it
Opening the file without newline=""The docs warn that "newlines embedded inside quoted fields will not be interpreted correctly"Always pass newline=""

Troubleshooting​

SymptomLikely causeFix
KeyError: 'Name'Keys match the header exactly, capital letters includedUse the name as the header writes it
The first person is missing, and the keys are their valuesThe file has no header line, so its first row became the column namesPass the names yourself: csv.DictReader(file, fieldnames=["name", "city", "age"])

FAQ​

How do I loop without loading the whole file?​

Loop over the reader inside the with block: for row in csv.DictReader(file):. It reads one line at a time. Printing row["name"], int(row["age"]) + 1 that way gives Ana 35 and Ben 29.

What if my file has no header line?​

Pass the column names: csv.DictReader(file, fieldnames=["name", "city", "age"]). Then the first line is read as data, not as names. For a file holding only Ana,Lisbon,34, that gives [{'name': 'Ana', 'city': 'Lisbon', 'age': '34'}].

Conclusion​

csv.DictReader turned each line of people.csv into a dictionary keyed by the header, so row["age"] reads like the file does. Every value is a string, so convert numbers before you calculate with them.

Additional Resources​

  • csv.DictReader — the class and its fieldnames, restkey and restval parameters.
  • csv.reader — the reader DictReader builds on, and the note on type conversion.