How to Read a CSV File into Dictionaries in Python with csv.DictReader
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
import csv
with open(<file name>, newline="", encoding="utf-8") as file:
for row in csv.DictReader(file):
<use row[<column name>]>
| Part | What 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:
name,city,age
Ana,Lisbon,34
Ben,Leeds,28
Step 2: Write the Script
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"]))

list(csv.DictReader(file))reads every row while the file is open.rows[0]is the first person, androws[0]["name"]is the value in theirnamecolumn.type(...)shows what kind of value the age is.
Step 3: Run It
uv run python read_people.py
[{'name': 'Ana', 'city': 'Lisbon', 'age': '34'}, {'name': 'Ben', 'city': 'Leeds', 'age': '28'}]
Ana is 34
<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
| Mistake | What happens | Do this instead |
|---|---|---|
Doing math on a value, such as rows[0]["age"] + 1 | TypeError: can only concatenate str (not "int") to str | Convert it first: int(rows[0]["age"]) + 1 gives 35 |
Making the reader inside the with block and looping over it after the block | ValueError: 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
| Symptom | Likely cause | Fix |
|---|---|---|
KeyError: 'Name' | Keys match the header exactly, capital letters included | Use the name as the header writes it |
| The first person is missing, and the keys are their values | The file has no header line, so its first row became the column names | Pass 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,restkeyandrestvalparameters. - csv.reader — the reader
DictReaderbuilds on, and the note on type conversion.
