15.7. CSV Recap
15.7.1. Assignments
# %% About
# - Name: CSV Recap Iris
# - Difficulty: easy
# - Lines: 3
# - 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. Using `csv.writer()` save `DATA` to file
# 2. Use Unix `\n` line terminator
# 3. Run doctests - all must succeed
# %% Polish
# 1. Za pomocą `csv.writer()` zapisz `DATA` do pliku
# 2. Użyj zakończenia linii Unix `\n`
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Example
# firstname,lastname,age
# Alice,Apricot,30
# Bob,Banana,31
# Carol,Corn,32
# Dave,Durian,33
# Eve,Elderberry,34
# Mallory,Melon,15
# <BLANKLINE>
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> result = open(FILE).read()
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is str, \
'Variable `result` has invalid type, should be str'
>>> from os import remove
>>> remove(FILE)
>>> print(result)
firstname,lastname,age
Alice,Apricot,30
Bob,Banana,31
Carol,Corn,32
Dave,Durian,33
Eve,Elderberry,34
Mallory,Melon,15
<BLANKLINE>
"""
# %% 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
# %% Data
FILE = r'_temporary.csv'
DATA = [
{'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},
]
# %% Result
with open(FILE, mode='wt', encoding='utf-8') as file:
...
# %% About
# - Name: CSV Recap Iris
# - Difficulty: easy
# - Lines: 3
# - 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. Using `csv.writer()` save `DATA` to file
# 2. Use Unix `\n` line terminator
# 3. Run doctests - all must succeed
# %% Polish
# 1. Za pomocą `csv.writer()` zapisz `DATA` do pliku
# 2. Użyj zakończenia linii Unix `\n`
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Example
# >>> result
# firstname,lastname,age
# Alice,Apricot,30
# Bob,Banana,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 3.9+ required'
>>> result = open(FILE).read()
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is str, \
'Variable `result` has invalid type, should be str'
>>> from os import remove
>>> remove(FILE)
>>> print(result)
firstname,lastname,age
Alice,Apricot,30
Bob,Banana,31
Carol,Corn,32
Dave,Durian,33
Eve,Elderberry,34
Mallory,Melon,15
<BLANKLINE>
"""
# %% 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
# %% Data
FILE = r'_temporary.csv'
class User:
def __init__(self, firstname, lastname, age):
self.firstname = firstname
self.lastname = lastname
self.age = age
DATA = [
User('Alice', 'Apricot', age=30),
User('Bob', 'Banana', age=31),
User('Carol', 'Corn', age=32),
User('Dave', 'Durian', age=33),
User('Eve', 'Elderberry', age=34),
User('Mallory', 'Melon', age=15),
]
# %% Result
with open(FILE, mode='wt', encoding='utf-8') as file:
...
# %% About
# - Name: CSV Recap Syntax
# - Difficulty: easy
# - Lines: 4
# - 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. Using `csv.reader()` read data from `FILE`
# 2. Define `result: list[tuple]` with converted data
# 3. Use Unix `\n` line terminator
# 4. Convert values to proper types, ie. str, int, float
# 5. Run doctests - all must succeed
# %% Polish
# 1. Używając `csv.reader()` wczytaj dane z `FILE`
# 2. Zdefiniuj `result: list[tuple]` z przekonwertowanymi danymi
# 3. Użyj zakończenia linii Unix `\n`
# 4. Przekonwertuj wartości do odpowiednich typów, np. str, int, float
# 5. Uruchom doctesty - wszystkie muszą się powieść
# %% Example
# [('firstname', 'lastname', 'age'),
# ('Alice', 'Apricot', 30),
# ('Bob', 'Banana', 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 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 tuple for x in result), \
'All rows in `result` should be tuple'
>>> from os import remove
>>> remove(FILE)
>>> from pprint import pprint
>>> pprint(result)
[('firstname', 'lastname', 'age'),
('Alice', 'Apricot', 30),
('Bob', 'Banana', 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 -v myfile.py`
# %% Imports
import csv
# %% Types
result: list[tuple[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 = ...
# %% About
# - Name: CSV Recap Enumerate
# - Difficulty: medium
# - Lines: 8
# - Minutes: 8
# %% 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[tuple]` with converted data
# 3. Use Unix `\n` line terminator
# 4. Run doctests - all must succeed
# %% Polish
# 1. Za pomocą `csv.reader()` wczytaj dane z `FILE`
# 2. Zdefiniuj `result: list[tuple]` z przekonwertowanymi danymi
# 3. Użyj zakończenia linii Unix `\n`
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Example
# >>> result
# [('Alice', 'Apricot', 'users'),
# ('Bob', 'Banana', 'users'),
# ('Carol', 'Corn', 'staff'),
# ('Dave', 'Durian', 'staff'),
# ('Eve', 'Elderberry', 'admins'),
# ('Mallory', 'Melon', 'admins')]
# %% 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 tuple for x in result), \
'All rows in `result` should be tuple'
>>> from os import remove
>>> remove(FILE)
>>> from pprint import pprint
>>> pprint(result)
[('Alice', 'Apricot', 'users'),
('Bob', 'Banana', 'users'),
('Carol', 'Corn', 'staff'),
('Dave', 'Durian', 'staff'),
('Eve', 'Elderberry', 'admins'),
('Mallory', 'Melon', 'admins')]
"""
# %% 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[tuple[str,str,str]]
# %% Data
FILE = r'_temporary.csv'
DATA = """
6,2,users,staff,admins
Alice,Apricot,0
Bob,Banana,0
Carol,Corn,1
Dave,Durian,1
Eve,Elderberry,2
Mallory,Melon,2
"""
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 Recap Schemaless
# - Difficulty: medium
# - Lines: 7
# - 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. Using `csv.DictWriter()` write variable schema data to `FILE`
# 2. `fieldnames` must be automatically generated from `DATA`
# 3. Non functional requirements:
# - All fields must be enclosed by double quote `"` character
# - Use `,` to separate columns
# - Use `utf-8` encoding
# - Use Unix `\n` line terminator
# - Sort `fieldnames` using `sorted()`
# 4. Run doctests - all must succeed
# %% Polish
# 1. Za pomocą `csv.DictWriter()` zapisz dane o zmiennej strukturze do `FILE`
# 2. `fieldnames` musi być generowane automatycznie na podstawie `DATA`
# 3. Wymagania niefunkcjonalne:
# - Wszystkie pola muszą być otoczone znakiem cudzysłowu `"`
# - Użyj `,` do oddzielenia kolumn
# - Użyj kodowania `utf-8`
# - Użyj zakończenia linii Unix `\n`
# - Posortuj `fieldnames` używając `sorted()`
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Example
# >>> result
# "age","firstname","lastname"
# "","Alice","Apricot"
# "31","Bob",""
# "","Carol","Corn"
# "33","","Durian"
# "34","Eve",""
# "15","","Mallory"
# <BLANKLINE>
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> result = open(FILE).read()
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is str, \
'Variable `result` has invalid type, should be str'
>>> from os import remove
>>> remove(FILE)
>>> print(result)
"age","firstname","lastname"
"","Alice","Apricot"
"31","Bob",""
"","Carol","Corn"
"33","","Durian"
"34","Eve",""
"15","","Mallory"
<BLANKLINE>
"""
# %% 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
# %% Data
FILE = r'_temporary.csv'
DATA = [
{'firstname': 'Alice', 'lastname': 'Apricot'},
{'firstname': 'Bob', 'age': 31},
{'lastname': 'Corn', 'firstname': 'Carol'},
{'lastname': 'Durian', 'age': 33},
{'age': 34, 'firstname': 'Eve'},
{'age': 15, 'lastname': 'Mallory', },
]
# %% Result
with open(FILE, mode='wt', encoding='utf-8') as file:
...
# %% About
# - Name: CSV Recap 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. Convert values to `int`
# 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. Skonwertuj wartości na `int`
# 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 = ...
# %% About
# - Name: CSV Relations Nested
# - Difficulty: hard
# - Lines: 14
# - Minutes: 13
# %% 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. Convert `DATA` to format with one column per each attribute for example:
# - `mission1_year`, `mission2_year`,
# - `mission1_name`, `mission2_name`
# 2. Note, that enumeration starts with one
# 3. Sort `fieldnames`
# 4. Save data to `FILE`
# 5. Run doctests - all must succeed
# %% Polish
# 1. Przekonwertuj `DATA` do formatu z jedną kolumną dla każdego atrybutu, np:
# - `mission1_year`, `mission2_year`,
# - `mission1_name`, `mission2_name`
# 2. Zwróć uwagę, że enumeracja zaczyna się od jeden
# 3. Posortuj `fieldnames`
# 4. Zapisz dane do `FILE`
# 5. Uruchom doctesty - wszystkie muszą się powieść
# %% Example
# >>> result
# "firstname","group1_gid","group1_name","group2_gid","group2_name","group3_gid","group3_name","lastname"
# "Alice","1","users","2","staff","","","Apricot"
# "Bob","1","users","2","staff","","","Banana"
# "Carol","1","users","","","","","Corn"
# "Dave","1","users","","","","","Durian"
# "Eve","1","users","2","staff","3","admins","Elderberry"
# "Mallory","","","","","","","Melon"
# <BLANKLINE>
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> result = open(FILE).read()
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is str, \
'Variable `result` has invalid type, should be str'
>>> from os import remove
>>> remove(FILE)
>>> print(result)
"firstname","group1_gid","group1_name","group2_gid","group2_name","group3_gid","group3_name","lastname"
"Alice","1","users","2","staff","","","Apricot"
"Bob","1","users","2","staff","","","Banana"
"Carol","1","users","","","","","Corn"
"Dave","1","users","","","","","Durian"
"Eve","1","users","2","staff","3","admins","Elderberry"
"Mallory","","","","","","","Melon"
<BLANKLINE>
"""
# %% 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
# %% Data
FILE = r'_temporary.csv'
DATA = [
{"firstname": "Alice", "lastname": "Apricot", "groups": [
{"gid": 1, "name": "users"},
{"gid": 2, "name": "staff"},
]},
{"firstname": "Bob", "lastname": "Banana", "groups": [
{"gid": 1, "name": "users"},
{"gid": 2, "name": "staff"},
]},
{"firstname": "Carol", "lastname": "Corn", "groups": [
{"gid": 1, "name": "users"},
]},
{"firstname": "Dave", "lastname": "Durian", "groups": [
{"gid": 1, "name": "users"},
]},
{"firstname": "Eve", "lastname": "Elderberry", "groups": [
{"gid": 1, "name": "users"},
{"gid": 2, "name": "staff"},
{"gid": 3, "name": "admins"},
]},
{"firstname": "Mallory", "lastname": "Melon", "groups": []},
]
# %% Result
# %% About
# - Name: CSV Relations Join
# - Difficulty: hard
# - Lines: 11
# - Minutes: 13
# %% 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.DictWriter()` save `DATA` to `FILE`
# 2. Non-functional requirements:
# - All fields must be enclosed by double quote `"` character
# - Use `,` to separate mission fields
# - Use `;` to separate missions
# - Use Unix `\n` newline
# - Sort `fieldnames` using `sorted()`
# 3. Run doctests - all must succeed
# %% Polish
# 1. Za pomocą `csv.DictWriter()` zapisz `DATA` do `FILE`
# 2. Wymagania niefunkcjonalne:
# - Wszystkie pola muszą być otoczone znakiem cudzysłowu `"`
# - Użyj `,` do oddzielania pól mission
# - Użyj `;` do oddzielenia missions
# - Użyj zakończenia linii Unix `\n`
# - Posortuj `fieldnames` używając `sorted()`
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `vars(obj)`
# - Nested `for`
# - `str.join(';', sequence)`
# - `str.join(',', sequence)`
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> result = open(FILE).read()
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is str, \
'Variable `result` has invalid type, should be str'
>>> from os import remove
>>> remove(FILE)
>>> print(result)
"firstname","groups","lastname"
"Mark","1,users","Watney"
"Melissa","1,users;2,admins","Lewis"
"Rick","","Martinez"
<BLANKLINE>
"""
# %% 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
# %% Data
FILE = r'_temporary.csv'
class Group:
gid: int
name: str
def __init__(self, gid, name):
self.gid = gid
self.name = name
class User:
firstname: str
lastname: str
groups: list[Group]
def __init__(self, firstname, lastname, groups=None):
self.firstname = firstname
self.lastname = lastname
self.groups = list(groups) if groups else []
DATA = [
User('Mark', 'Watney', groups=[
Group(gid=1, name='users')]),
User('Melissa', 'Lewis', groups=[
Group(gid=1, name='users'),
Group(gid=2, name='admins')]),
User('Rick', 'Martinez', groups=[]),
]
# %% Result