Python Programming home
Department of Computing, Imperial College London
Python for C++ Programmers
Chapter 1: Introduction
Chapter 2: Basic data types
Chapter 3: Variables and operators
Chapter 4: Sequence types
Chapter 5: Sets and dictionaries
Chapter 6: Control flow
Chapter 7: Functions
Chapter 8: Object-oriented programming
Chapter 9: Modules
Chapter 10: Files
[10.1] Handling text files
[10.2] JSON files
[10.3] Loading JSON files
[10.4] Writing to JSON files
[10.5] pickle
[10.6] Pickling time!
[10.7] CSV files
[10.8] Reading CSV files
[10.9] Reading CSV files into a dict
[10.10] Writing to CSV files
[10.11] That's a wrap!
Chapter 10: Files
>> pickle
face Josiah Wang
If you need to save Python data structures that are more complex, and only expect to load them in the future
into Python, you can also consider using Python’s pickle module.
Image by Alina Kuptsova from Pixabay
The pickle module is used for storing Python object structures into a file (and retrieving it back into memory at a
later time).
For example, you may use it to save your Machine Learning model that you have been spending the whole
week training.
You pickle your Python objects onto the disk as a binary file (serialisation), and you unpickle them from the disk
into memory (deserialisation).
You can pickle integers, floats, booleans, strings, tuples, lists, sets, dictionaries (that contain objects that can be
pickled), classes, and functions. No pickled gherkins, sorry!
Health warnings!
pickle is specific to Python. You probably should not use it if you need to share your data across different
programming languages
Make sure you use the same Python version. Pickle might not work correctly with different versions of
Python
Do not unpickle data from untrusted sources. There might be malicious code inside the file!
Previous Next
Page designed by Josiah Wang Department of Computing | Imperial College London