This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 7: More file handling

pickle

face Josiah Wang

In Lesson 9, we looked at how you can load Python data structures from a JSON file, and also save them to a JSON file.

If you need to save Python data structures that are more complex, and only expect to load them in Python in the future, you can also consider using Python’s pickle module.

Pickle

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 spent 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!