Patternite Logo

Save and load Python objects using `pickle`

0

A simple pattern for serializing and deserializing a Python object, allowing it to be saved to, and loaded from, file

Loading...

Sometime, we want to save a Python object to file. This is particularly useful in situations where we want to use this object in other code. Rather than having to write the object into, say, a text file, only to have to parse out the object again later, we can simply pickle the object. This will create a file which can be loaded (or "unpickled") into another script, restoring the original object.

Note the use of "wb" on line #6 and "rb" on line #10. These stand for "write binary" and "read binary" respectively, which are necessary since the pickle file stores the pickled object in binary format.

You can read more about pickling in the Python docs.

Profile picture for duncster

Patternite © 2021

Patternite Logo