17.3. CSV Reader
Reads CSV file to list[list]
csv.reader()
Default encoding is
encoding='utf-8'
17.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
17.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']]
17.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']]
17.3.4. Assignments
# %% 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
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: CSV Reader Syntax
# - Difficulty: easy
# - Lines: 4
# - Minutes: 5
# %% 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 przekonwerowanymi danymi
# 3. Użyj zakończenia linii Unix `\n`
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> 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'),
('Mark', 'Watney', '42'),
('Melissa', 'Lewis', '41'),
('Rick', 'Martinez', '40'),
('Alex', 'Vogel', '42'),
('Beth', 'Johanssen', '29'),
('Chris', 'Beck', '36')]
"""
import csv
FILE = r'_temporary.csv'
DATA = """
firstname,lastname,age
Mark,Watney,42
Melissa,Lewis,41
Rick,Martinez,40
Alex,Vogel,42
Beth,Johanssen,29
Chris,Beck,36
"""
with open(FILE, mode='wt', encoding='utf-8') as file:
file.write(DATA.lstrip())
# Using `csv.reader()` read data from `FILE`
# Define `result: list[tuple]` with converted data
# Use Unix `\n` line terminator
# type: list[tuple]
with open(FILE, mode='rt', encoding='utf-8') as file:
result = ...
# %% 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
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: CSV Reader Syntax
# - Difficulty: easy
# - Lines: 4
# - Minutes: 5
# %% 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 przekonwerowanymi danymi
# 3. Użyj zakończenia linii Unix `\n`
# 4. Użyj delimiter `;`
# 5. Użyj quotechar `'`
# 6. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> 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'),
('Mark', 'Watney', '42'),
('Melissa', 'Lewis', '41'),
('Rick', 'Martinez', '40'),
('Alex', 'Vogel', '42'),
('Beth', 'Johanssen', '29'),
('Chris', 'Beck', '36')]
"""
import csv
FILE = r'_temporary.csv'
DATA = """
'firstname';'lastname';'age'
'Mark';'Watney';42
'Melissa';'Lewis';41
'Rick';'Martinez';40
'Alex';'Vogel';42
'Beth';'Johanssen';29
'Chris';'Beck';36
"""
with open(FILE, mode='wt', encoding='utf-8') as file:
file.write(DATA.lstrip())
# Using `csv.reader()` read data from `FILE`
# Define `result: list[tuple]` with converted data
# Use Unix `\n` line terminator
# Use delimiter `;`
# Use quotechar `'`
# type: list[tuple]
with open(FILE, mode='rt', encoding='utf-8') as file:
result = ...
# %% 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
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: CSV Reader Syntax
# - Difficulty: easy
# - Lines: 4
# - Minutes: 5
# %% 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 przekonwerowanymi 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ść
# %% Tests
"""
>>> 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'),
('Mark', 'Watney', 42),
('Melissa', 'Lewis', 41),
('Rick', 'Martinez', 40),
('Alex', 'Vogel', 42),
('Beth', 'Johanssen', 29),
('Chris', 'Beck', 36)]
"""
import csv
FILE = r'_temporary.csv'
DATA = """
firstname,lastname,age
Mark,Watney,42
Melissa,Lewis,41
Rick,Martinez,40
Alex,Vogel,42
Beth,Johanssen,29
Chris,Beck,36
"""
with open(FILE, mode='wt', encoding='utf-8') as file:
file.write(DATA.lstrip())
# Using `csv.reader()` read data from `FILE`
# Define `result: list[tuple]` with converted data
# Use Unix `\n` line terminator
# Convert values to proper types, ie. str, int, float
# type: list[tuple]
with open(FILE, mode='rt', encoding='utf-8') as file:
result = ...
# %% 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
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: CSV Reader Enumerate
# - Difficulty: medium
# - Lines: 8
# - Minutes: 8
# %% 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 przekonwerowanymi danymi
# 3. Użyj zakończenia linii Unix `\n`
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> 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)
[('Mark', 'Watney', 'staff'),
('Melissa', 'Lewis', 'admins'),
('Rick', 'Martinez', 'staff'),
('Alex', 'Vogel', 'users'),
('Beth', 'Johanssen', 'staff'),
('Chris', 'Beck', 'staff')]
"""
import csv
FILE = r'_temporary.csv'
DATA = """
6,2,users,staff,admins
Mark,Watney,1
Melissa,Lewis,2
Rick,Martinez,1
Alex,Vogel,0
Beth,Johanssen,1
Chris,Beck,1
"""
with open(FILE, mode='wt', encoding='utf-8') as file:
file.write(DATA.lstrip())
# data from file (note the list[tuple] format!)
# type: list[tuple]
with open(FILE, mode='rt', encoding='utf-8') as file:
result = ...