#94: Store Your Objects With Pickle
Sometimes the collection of data takes a lot longer than processing it. Wouldn't it be great if you could store whole Python objects? If something goes wrong, we can restart the analytics part and don't need to recreate the data.
What is pickle?
The pickle module implements binary protocols for serializing and de-serializing a Python object structure. “Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream (from a binary file or bytes-like object) is converted back into an object hierarchy. (Source: docs.python.org)
Pickle is a great helper to store (temporarily) your data. Be aware that a lot is going on when you unpickle data that could compromise your computer – therefore, only use it with trusted sources!
Save your objects
We can use the same example data from the post on PrettyPrinter:
For pickle we need a file that we open in the binary mode. The dump() method does all the work and persists our objects:
The pickle.bin file now contains our objects in a binary format. If we open the file in a text editor, we see a lot of special characters:

Restore your objects
Pickle offers the load() method to restore our objects from a file (that we need to open in binary mode):
For our application, the restored objects look the same as if we would create them in code. We can run our script and print the values to the command line or do whatever we want with them:
Conclusion
The pickle module in Python offers a built-in way to persist whole object graphs without much effort. If you need to temporarily store your objects you should try pickle.