15.5. CSV DictReader
Reads CSV file to list[dict]
csv.DictReader()
15.5.1. SetUp
>>> import csv
>>> from pprint import pprint
>>> DATA = """
...
... "firstname","lastname","age"
... "Alice","Apricot","30"
... "Bob","Banana","31"
... "Carol","Corn","32"
... "Dave","Durian","33"
... "Eve","Elderberry","34"
... "Mallory","Melon","15"
...
... """
>>>
>>> with open('/tmp/myfile.csv', mode='wt') as file:
... file.write(DATA.strip())
159
15.5.2. Minimal
Data:
$ cat /tmp/myfile.csv
"firstname","lastname","age"
"Alice","Apricot","30"
"Bob","Banana","31"
"Carol","Corn","32"
"Dave","Durian","33"
"Eve","Elderberry","34"
"Mallory","Melon","15"
Usage:
>>> with open('/tmp/myfile.csv', mode='rt') as file:
... reader = csv.DictReader(file)
... result = list(reader)
Result:
>>> pprint(result, sort_dicts=False)
[{'firstname': 'Alice', 'lastname': 'Apricot', 'age': '30'},
{'firstname': 'Bob', 'lastname': 'Banana', 'age': '31'},
{'firstname': 'Carol', 'lastname': 'Corn', 'age': '32'},
{'firstname': 'Dave', 'lastname': 'Durian', 'age': '33'},
{'firstname': 'Eve', 'lastname': 'Elderberry', 'age': '34'},
{'firstname': 'Mallory', 'lastname': 'Melon', 'age': '15'}]
15.5.3. Parametrized
Data:
$ cat /tmp/myfile.csv
"firstname","lastname","age"
"Alice","Apricot","30"
"Bob","Banana","31"
"Carol","Corn","32"
"Dave","Durian","33"
"Eve","Elderberry","34"
"Mallory","Melon","15"
Usage:
>>> with open('/tmp/myfile.csv', mode='rt', encoding='utf-8') as file:
... reader = csv.DictReader(file, delimiter=',', quoting=csv.QUOTE_ALL, quotechar='"', lineterminator='\n')
... result = list(reader)
Result:
>>> pprint(result, sort_dicts=False)
[{'firstname': 'Alice', 'lastname': 'Apricot', 'age': '30'},
{'firstname': 'Bob', 'lastname': 'Banana', 'age': '31'},
{'firstname': 'Carol', 'lastname': 'Corn', 'age': '32'},
{'firstname': 'Dave', 'lastname': 'Durian', 'age': '33'},
{'firstname': 'Eve', 'lastname': 'Elderberry', 'age': '34'},
{'firstname': 'Mallory', 'lastname': 'Melon', 'age': '15'}]
15.5.4. Custom Header
Read data from CSV file using csv.DictReader()
. While giving custom names
note, that first line (typically a header) will be treated like normal data.
Therefore we skip it using header = file.readline()
:
>>> fieldnames = ['fname', 'lname', 'age']
>>>
>>> with open('/tmp/myfile.csv', mode='rt') as file:
... reader = csv.DictReader(file, fieldnames)
... old_header = next(reader)
... result = list(reader)
Result:
>>> pprint(result, sort_dicts=False)
[{'fname': 'Alice', 'lname': 'Apricot', 'age': '30'},
{'fname': 'Bob', 'lname': 'Banana', 'age': '31'},
{'fname': 'Carol', 'lname': 'Corn', 'age': '32'},
{'fname': 'Dave', 'lname': 'Durian', 'age': '33'},
{'fname': 'Eve', 'lname': 'Elderberry', 'age': '34'},
{'fname': 'Mallory', 'lname': 'Melon', 'age': '15'}]
15.5.5. Use Case - 1
sepal_length,sepal_width,petal_length,petal_width,species
5.8,2.7,5.1,1.9,virginica
5.1,3.5,1.4,0.2,setosa
5.7,2.8,4.1,1.3,versicolor
>>> import csv
>>> from pathlib import Path
>>> from pprint import pprint
>>>
>>>
>>> DATA = """sepal_length,sepal_width,petal_length,petal_width,species
... 5.8,2.7,5.1,1.9,virginica
... 5.1,3.5,1.4,0.2,setosa
... 5.7,2.8,4.1,1.3,versicolor
... """
>>>
>>> _ = Path('/tmp/myfile.csv').write_text(DATA)
>>>
>>>
>>> def clean(row: dict) -> dict:
... return {
... 'sepal_length': float(row['sepal_length']),
... 'sepal_width': float(row['sepal_width']),
... 'petal_length': float(row['petal_length']),
... 'petal_width': float(row['petal_width']),
... 'species': row['species']
... }
>>>
>>>
>>> with open('/tmp/myfile.csv') as file:
... reader = csv.DictReader(file)
... result = map(clean, reader)
... result = list(result)
>>>
>>> pprint(result, sort_dicts=False)
[{'sepal_length': 5.8,
'sepal_width': 2.7,
'petal_length': 5.1,
'petal_width': 1.9,
'species': 'virginica'},
{'sepal_length': 5.1,
'sepal_width': 3.5,
'petal_length': 1.4,
'petal_width': 0.2,
'species': 'setosa'},
{'sepal_length': 5.7,
'sepal_width': 2.8,
'petal_length': 4.1,
'petal_width': 1.3,
'species': 'versicolor'}]
15.5.6. Assignments
# %% About
# - Name: CSV DictReader Iris
# - Difficulty: easy
# - Lines: 5
# - Minutes: 5
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% English
# 1. Define `result: list[dict]`
# 2. To `result` add data read from `FILE`
# 3. Use `csv.DictReader` to parse file
# 4. Do not convert values to `int`, leave as `str`
# 5. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: list[dict]`
# 2. Do `result` dodaj wczytane dane z pliku `FILE`
# 3. Użyj `csv.DictReader` do sparsowania pliku
# 4. Nie konwertuj wartości na `int`, pozostaw jako `str`
# 5. Uruchom doctesty - wszystkie muszą się powieść
# %% Example
# >>> result
# [{'firstname': 'Alice', 'lastname': 'Apricot', 'age': '30'},
# {'firstname': 'Bob', 'lastname': 'Banana', 'age': '31'},
# {'firstname': 'Carol', 'lastname': 'Corn', 'age': '32'},
# {'firstname': 'Dave', 'lastname': 'Durian', 'age': '33'},
# {'firstname': 'Eve', 'lastname': 'Elderberry', 'age': '34'},
# {'firstname': 'Mallory', 'lastname': 'Melon', 'age': '15'}]
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is list, \
'Variable `result` has invalid type, should be list'
>>> assert all(type(x) is dict for x in result), \
'All rows in `result` should be dict'
>>> from os import remove
>>> remove(FILE)
>>> from pprint import pprint
>>> pprint(result, sort_dicts=False)
[{'firstname': 'Alice', 'lastname': 'Apricot', 'age': '30'},
{'firstname': 'Bob', 'lastname': 'Banana', 'age': '31'},
{'firstname': 'Carol', 'lastname': 'Corn', 'age': '32'},
{'firstname': 'Dave', 'lastname': 'Durian', 'age': '33'},
{'firstname': 'Eve', 'lastname': 'Elderberry', 'age': '34'},
{'firstname': 'Mallory', 'lastname': 'Melon', 'age': '15'}]
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% Imports
import csv
# %% Types
result: list[dict[str,str,int]]
# %% Data
FILE = r'_temporary.csv'
DATA = """
firstname,lastname,age
Alice,Apricot,30
Bob,Banana,31
Carol,Corn,32
Dave,Durian,33
Eve,Elderberry,34
Mallory,Melon,15
"""
with open(FILE, mode='wt', encoding='utf-8') as file:
file.write(DATA.lstrip())
# %% Result
with open(FILE, mode='rt', encoding='utf-8') as file:
result = ...