15.3. CSV Reader
Reads CSV file to list[list]
csv.reader()
Default encoding is
encoding='utf-8'
15.3.1. SetUp
>>> import csv
>>> from pprint import pprint
>>> DATA = """
...
... "firstname","lastname","age"
... "Mark","Watney","42"
... "Melissa","Lewis","41"
... "Rick","Martinez","40"
... "Alex","Vogel","42"
... "Beth","Johanssen","29"
... "Chris","Beck","36"
...
... """
>>>
>>> with open('/tmp/myfile.csv', mode='wt') as file:
... file.write(DATA.strip())
159
15.3.2. Minimal
Default mode is
mode='r'
Data:
$ cat /tmp/myfile.csv
"firstname","lastname","age"
"Mark","Watney","42"
"Melissa","Lewis","41"
"Rick","Martinez","40"
"Alex","Vogel","42"
"Beth","Johanssen","29"
"Chris","Beck","36"
Usage:
>>> with open('/tmp/myfile.csv', mode='rt') as file:
... reader = csv.reader(file)
... result = list(reader)
Result:
>>> pprint(result)
[['firstname', 'lastname', 'age'],
['Mark', 'Watney', '42'],
['Melissa', 'Lewis', '41'],
['Rick', 'Martinez', '40'],
['Alex', 'Vogel', '42'],
['Beth', 'Johanssen', '29'],
['Chris', 'Beck', '36']]
15.3.3. Parametrized
Data:
$ cat /tmp/myfile.csv
"firstname","lastname","age"
"Mark","Watney","42"
"Melissa","Lewis","41"
"Rick","Martinez","40"
"Alex","Vogel","42"
"Beth","Johanssen","29"
"Chris","Beck","36"
Usage:
>>> with open('/tmp/myfile.csv', mode='rt') as file:
... reader = csv.reader(file, delimiter=',', quoting=csv.QUOTE_ALL, quotechar='"', lineterminator='\n')
... result = list(reader)
Result:
>>> pprint(result)
[['firstname', 'lastname', 'age'],
['Mark', 'Watney', '42'],
['Melissa', 'Lewis', '41'],
['Rick', 'Martinez', '40'],
['Alex', 'Vogel', '42'],
['Beth', 'Johanssen', '29'],
['Chris', 'Beck', '36']]
15.3.4. Assignments
# %% About
# - Name: CSV Reader 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. 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. 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'
>>> 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,...]]
# %% 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 Reader 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. Use delimiter `;`
# 5. Use quotechar `'`
# 6. 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. Użyj delimiter `;`
# 5. Użyj quotechar `'`
# 6. 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,...]]
# %% 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 = ...