15.3. CSV Load
csv.reader()csv.DictReader()
15.3.1. SetUp
>>> import csv
>>> from pprint import pprint
>>> DATA = """
... firstname,lastname,age
... Alice,Apricot,30
... Bob,Blackthorn,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())
15.3.2. List of Sequences
Default mode is
mode='r'
Data:
$ cat /tmp/myfile.csv
firstname,lastname,age
Alice,Apricot,30
Bob,Blackthorn,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.reader(file)
... result = list(reader)
Result:
>>> pprint(result)
[['firstname', 'lastname', 'age'],
['Alice', 'Apricot', '30'],
['Bob', 'Blackthorn', '31'],
['Carol', 'Corn', '32'],
['Dave', 'Durian', '33'],
['Eve', 'Elderberry', '34'],
['Mallory', 'Melon', '15']]
15.3.3. List of Mappings
Data:
$ cat /tmp/myfile.csv
firstname,lastname,age
Alice,Apricot,30
Bob,Blackthorn,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': 'Blackthorn', '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.3.4. Assignments
# %% About
# - Name: CSV Reader Sequence
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2
# %% 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. Read data from file `FILE` in CSV format
# 2. Define `result` with the result
# 3. Use `csv` builtin module
# 4. Run doctests - all must succeed
# %% Polish
# 1. Wczytaj dane z pliku `FILE` w formacie CSV
# 2. Zdefiniuj `result` z wynikiem
# 3. Użyj wbudowanego modułu `csv`
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> result
# [['Alice', 'Apricot', '30']]
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is list, \
'Variable `result` has an invalid type; expected: `list`.'
>>> assert all(type(x) is list for x in result), \
'Variable `result` has elements of an invalid type; all items should be: `list`.'
>>> from os import remove
>>> remove(FILE)
>>> from pprint import pprint
>>> pprint(result)
[['Alice', 'Apricot', '30']]
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
import csv
# %% Types
result: list[tuple[str,...]]
# %% Data
FILE = r'_temporary.csv'
DATA = 'Alice,Apricot,30\n'
with open(FILE, mode='wt') as file:
file.write(DATA.lstrip())
# %% Result
with open(FILE, mode='rt') as file:
result = ...
# %% About
# - Name: CSV Reader ListSequence
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2
# %% 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. Read data from file `FILE` in CSV format
# 2. Define `result` with the result
# 3. Use `csv` builtin module
# 4. Run doctests - all must succeed
# %% Polish
# 1. Wczytaj dane z pliku `FILE` w formacie CSV
# 2. Zdefiniuj `result` z wynikiem
# 3. Użyj wbudowanego modułu `csv`
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> result
# [['firstname', 'lastname', 'age'],
# ['Alice', 'Apricot', '30'],
# ['Bob', 'Blackthorn', '31'],
# ['Carol', 'Corn', '32'],
# ['Dave', 'Durian', '33'],
# ['Eve', 'Elderberry', '34'],
# ['Mallory', 'Melon', '15']]
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is list, \
'Variable `result` has an invalid type; expected: `list`.'
>>> assert all(type(x) is list for x in result), \
'Variable `result` has elements of an invalid type; all items should be: `list`.'
>>> from os import remove
>>> remove(FILE)
>>> from pprint import pprint
>>> pprint(result)
[['firstname', 'lastname', 'age'],
['Alice', 'Apricot', '30'],
['Bob', 'Blackthorn', '31'],
['Carol', 'Corn', '32'],
['Dave', 'Durian', '33'],
['Eve', 'Elderberry', '34'],
['Mallory', 'Melon', '15']]
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
import csv
# %% Types
result: list[tuple[str,...]]
# %% Data
FILE = r'_temporary.csv'
DATA = """
firstname,lastname,age
Alice,Apricot,30
Bob,Blackthorn,31
Carol,Corn,32
Dave,Durian,33
Eve,Elderberry,34
Mallory,Melon,15
"""
with open(FILE, mode='wt') as file:
file.write(DATA.lstrip())
# %% Result
with open(FILE, mode='rt') as file:
result = ...
# %% About
# - Name: CSV DictReader Mapping
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2
# %% 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. Read data from file `FILE` in CSV format
# 2. Define `result` with the result
# 3. Use `csv` builtin module
# 4. Note, we want only one object (not a list)
# 5. Run doctests - all must succeed
# %% Polish
# 1. Wczytaj dane z pliku `FILE` w formacie CSV
# 2. Zdefiniuj `result` z wynikiem
# 3. Użyj wbudowanego modułu `csv`
# 4. Zauważ, że chcemy tylko jeden obiekt (nie listę)
# 5. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> result
# {'firstname': 'Alice', 'lastname': 'Apricot', 'age': '30'}
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is dict, \
'Variable `result` has an invalid type; expected: `dict`.'
>>> from os import remove
>>> remove(FILE)
>>> from pprint import pprint
>>> pprint(result, sort_dicts=False)
{'firstname': 'Alice', 'lastname': 'Apricot', 'age': '30'}
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -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
"""
with open(FILE, mode='wt') as file:
file.write(DATA.lstrip())
# %% Result
with open(FILE, mode='rt') as file:
result = ...
# %% About
# - Name: CSV DictReader ListMapping
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2
# %% 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. Read data from file `FILE` in CSV format
# 2. Define `result` with the result
# 3. Use `csv` builtin module
# 4. Run doctests - all must succeed
# %% Polish
# 1. Wczytaj dane z pliku `FILE` w formacie CSV
# 2. Zdefiniuj `result` z wynikiem
# 3. Użyj wbudowanego modułu `csv`
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> result
# [{'firstname': 'Alice', 'lastname': 'Apricot', 'age': '30'},
# {'firstname': 'Bob', 'lastname': 'Blackthorn', '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 has an is invalid version; expected: `3.9` or newer.'
>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is list, \
'Variable `result` has an invalid type; expected: `list`.'
>>> assert all(type(x) is dict for x in result), \
'Variable `result` has elements of an invalid type; all items 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': 'Blackthorn', '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 -f -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,Blackthorn,31
Carol,Corn,32
Dave,Durian,33
Eve,Elderberry,34
Mallory,Melon,15
"""
with open(FILE, mode='wt') as file:
file.write(DATA.lstrip())
# %% Result
with open(FILE, mode='rt') as file:
result = ...
# %% About
# - Name: CSV DictReader ListMapping
# - Difficulty: easy
# - Lines: 3
# - Minutes: 3
# %% 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. Read data from file `FILE` in CSV format
# 2. Define `result` with the result
# 3. Use `csv` builtin module
# 4. Note, we want list[User] not list[dict]
# 5. Run doctests - all must succeed
# %% Polish
# 1. Wczytaj dane z pliku `FILE` w formacie CSV
# 2. Zdefiniuj `result` z wynikiem
# 3. Użyj wbudowanego modułu `csv`
# 4. Zauważ, że chcemy list[User] a nie list[dict]
# 5. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> result
# [User(firstname='Alice', lastname='Apricot', age='30'),
# User(firstname='Bob', lastname='Blackthorn', age='31'),
# User(firstname='Carol', lastname='Corn', age='32'),
# User(firstname='Dave', lastname='Durian', age='33'),
# User(firstname='Eve', lastname='Elderberry', age='34'),
# User(firstname='Mallory', lastname='Melon', age='15')]
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is list, \
'Variable `result` has an invalid type; expected: `list`.'
>>> assert all(type(x) is User for x in result), \
'Variable `result` has elements of an invalid type; all items should be: `User`.'
>>> from os import remove
>>> remove(FILE)
>>> from pprint import pprint
>>> pprint(result, sort_dicts=False)
[User(firstname='Alice', lastname='Apricot', age='30'),
User(firstname='Bob', lastname='Blackthorn', age='31'),
User(firstname='Carol', lastname='Corn', age='32'),
User(firstname='Dave', lastname='Durian', age='33'),
User(firstname='Eve', lastname='Elderberry', age='34'),
User(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 -f -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,Blackthorn,31
Carol,Corn,32
Dave,Durian,33
Eve,Elderberry,34
Mallory,Melon,15
"""
with open(FILE, mode='wt') as file:
file.write(DATA.lstrip())
class User:
def __init__(self, firstname, lastname, age):
self.firstname = firstname
self.lastname = lastname
self.age = age
def __repr__(self):
clsname = self.__class__.__name__
firstname = self.firstname
lastname = self.lastname
age = self.age
return f'{clsname}({firstname=}, {lastname=}, {age=})'
# %% Result
with open(FILE, mode='rt') as file:
result = ...