15.3. Pickle Dump
File extension
pkl
rb
reading in binary modewb
writing in binary mode
15.3.1. SetUp
>>> import pickle
15.3.2. Serialize to File
>>> DATA = [1, 2, 3]
>>>
>>> with open('/tmp/myfile.pkl', mode='wb') as file:
... pickle.dump(DATA, file)
15.3.3. Deserialize from File
Load from file:
>>> with open('/tmp/myfile.pkl', mode='rb') as file:
... result = pickle.load(file)
>>>
>>> print(result)
[1, 2, 3]
15.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: Pickle Dump list[int]
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2
# %% English
# 1. Write `DATA` to `FILE`
# 2. Use `pickle` module
# 3. Run doctests - all must succeed
# %% Polish
# 1. Zapisz `DATA` do `FILE`
# 2. Użyj modułu `pickle`
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> with open(FILE, mode='rb') as file:
... result = pickle.load(file)
>>> from os import remove
>>> remove(FILE)
>>> from pprint import pprint
>>> pprint(result)
('Mark', 'Watney', 1)
"""
import pickle
FILE = r'_temporary.pkl'
DATA = ('Mark', 'Watney', 1)
# Write `DATA` to `FILE`
# Use `pickle` module
...
# %% 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: Pickle Dump list[tuple]
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2
# %% English
# 1. Write `DATA` to `FILE`
# 2. Use `pickle` module
# 3. Run doctests - all must succeed
# %% Polish
# 1. Zapisz `DATA` do `FILE`
# 2. Użyj modułu `pickle`
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> with open(FILE, mode='rb') as file:
... result = pickle.load(file)
>>> from os import remove
>>> remove(FILE)
>>> from pprint import pprint
>>> pprint(result)
[('Mark', 'Watney', 1),
('Melissa', 'Lewis', 3),
('Rick', 'Martinez', 1),
('Alex', 'Vogel', None),
('Chris', 'Beck', 1),
('Beth', 'Johanssen', 2)]
"""
import pickle
FILE = r'_temporary.pkl'
DATA = [
('Mark', 'Watney', 1),
('Melissa', 'Lewis', 3),
('Rick', 'Martinez', 1),
('Alex', 'Vogel', None),
('Chris', 'Beck', 1),
('Beth', 'Johanssen', 2),
]
# Write `DATA` to `FILE`
# Use `pickle` module
...
# %% 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: Pickle Dump list[dict]
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2
# %% English
# 1. Write `DATA` to `FILE`
# 2. Use `pickle` module
# 3. Run doctests - all must succeed
# %% Polish
# 1. Zapisz `DATA` do `FILE`
# 2. Użyj modułu `pickle`
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> with open(FILE, mode='rb') as file:
... result = pickle.load(file)
>>> from os import remove
>>> remove(FILE)
>>> from pprint import pprint
>>> pprint(result, sort_dicts=False)
[{'firstname': 'Mark', 'lastname': 'Watney', 'group': 1},
{'firstname': 'Melissa', 'lastname': 'Lewis', 'group': 3},
{'firstname': 'Rick', 'lastname': 'Martinez', 'group': 1},
{'firstname': 'Alex', 'lastname': 'Vogel', 'group': None},
{'firstname': 'Beth', 'lastname': 'Johanssen', 'group': 2},
{'firstname': 'Chris', 'lastname': 'Beck', 'group': 1}]
"""
import pickle
FILE = r'_temporary.pkl'
DATA = [
{'firstname': 'Mark', 'lastname': 'Watney', 'group': 1},
{'firstname': 'Melissa', 'lastname': 'Lewis', 'group': 3},
{'firstname': 'Rick', 'lastname': 'Martinez', 'group': 1},
{'firstname': 'Alex', 'lastname': 'Vogel', 'group': None},
{'firstname': 'Beth', 'lastname': 'Johanssen', 'group': 2},
{'firstname': 'Chris', 'lastname': 'Beck', 'group': 1},
]
# Write `DATA` to `FILE`
# Use `pickle` module
...