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: 4
# - 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. Using `csv.reader()` read data from `FILE`
# 2. Define `result: list[list]` with converted data
# 3. Use Unix `\n` line terminator
# 4. Run doctests - all must succeed

# %% Polish
# 1. Używając `csv.reader()` wczytaj dane z `FILE`
# 2. Zdefiniuj `result: list[list]` z przekonwertowanymi danymi
# 3. Użyj zakończenia linii Unix `\n`
# 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', encoding='utf-8') as file:
    file.write(DATA.lstrip())

# %% Result
with open(FILE, mode='rt', encoding='utf-8') as file:
    result = ...

# %% About
# - Name: CSV Reader ListSequence
# - Difficulty: easy
# - Lines: 4
# - 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. Using `csv.reader()` read data from `FILE`
# 2. Define `result: list[list]` with converted data
# 3. Use Unix `\n` line terminator
# 4. Run doctests - all must succeed

# %% Polish
# 1. Używając `csv.reader()` wczytaj dane z `FILE`
# 2. Zdefiniuj `result: list[list]` z przekonwertowanymi danymi
# 3. Użyj zakończenia linii Unix `\n`
# 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', encoding='utf-8') as file:
    file.write(DATA.lstrip())

# %% Result
with open(FILE, mode='rt', encoding='utf-8') as file:
    result = ...

# %% About
# - Name: CSV DictReader Iris
# - Difficulty: easy
# - Lines: 5
# - 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. 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ść

# %% 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', encoding='utf-8') as file:
    file.write(DATA.lstrip())

# %% Result
with open(FILE, mode='rt', encoding='utf-8') as file:
    result = ...