How to use Python pickle to serialize and deserialize objects
With Python pickle
it is possible to serialize and later deserialize objects. Numerous data types can be considered for conversion. However, as malicious code can also be stored in a memory file, you should only convert files from trustworthy sources into the original format.
What is Python pickle
?
Python pickle
may seem to have an unusual name at first, but if you take a closer look at how the module works and what it is used for, you will quickly understand why it was given this name. The module allows you to save objects (i.e. “preserve them”, hence “pickle”) in order to use them at a later time for another project. To do this, the objects are converted into a saveable format. This practice is called serializing. Python pickle
can also be used to deserialize objects, i.e. to convert them back to their original format. Python pickle
is particularly useful if you want to use objects frequently.
The object is converted into a byte stream, whereby all information is transferred unchanged. In addition, Python pickle
provides instructions for successful deserialization, through which the original structure can be reconstructed down to the smallest detail. Using Python pickle
saves a lot of time, as once an object is created, it does not have to be recreated for each use. The format for saving is .pkl.
Which data types can be converted?
Python pickle
can serialize the following data types:
- Boolean values: “true” and “false”, also “none”
- Integers and complex numbers
- Strings (normal and Unicode)
- Lists
- Sets
- Python tuples
- Directories consisting exclusively of corresponding objects
- Functions
- Python classes
What are the different methods?
There are four methods for working with Python pickle
, which are provided within the module:
pickle.dump(obj, file, protocol=None, *, fix_imports=True, buffer_callback=None)
: Is used for serialization and creates a file with the desired resultpickle.dumps(obj, protocol=None, *, fix_imports=True, buffer_callback=None)
: Is also used for serialization, but returns a byte stringpickle.load(file, *, fix_imports=True, encoding='ASCII', errors="strict", buffers=None)
: Is used for deserialization and reads the saved file for this purposepickle.loads(bytes_object, *, fix_imports=True, encoding="ASCII", errors="strict", buffers=None)
: Is also used for deserialization, but operates with a byte string
To differentiate between the methods, you can remember that the “s” in pickle.dumps
and pickle.loads
stands for “String.”
How to use Python pickle
with an example
To better illustrate how Python pickle
works, we will work with a simple example. We will create a simple list containing four colors. This is our code:
Then we open a text file in the .pkl format and use pickle.dump()
to save our list there. This is the code we use for this:
The abbreviation wb
instructs the system to open the file in binary form. This also outputs the contained data as a bytes object. The “colors” list is then saved in this file with dump()
. Finally, the file is closed automatically.
How to convert a saved file to its original format
If you now want to deserialize a binary file again, use the Python method pickle.load()
. With the following code, it is possible to convert the object back to its original format and initiate an output. We add the abbreviation rb
, which stands for “read binary”.
This gives us the following output:
How to serialize dictionary with Python pickle
You can also easily serialize more complex data types such as directories with Python pickle
and then convert them back to their original form. To do this, we first create a directory with the name “persons”. Here we store different data for different people:
In the following code, we create a new file, convert the data, and then test by converting it back to serialize this directory:
The resulting output looks like this:
You can now access the information as usual. We request the following output as an example:
This is what our output looks like:
How to convert a class to a string
In the next example, we use Python pickle
to save a class in a string. This class contains completely different data types, but all of them can be taken into account. We create a class called “ExampleClass” and then serialize it. The corresponding code is:
After serializing the class and then converting it back to its original format, we get this output:
How to compress serialized objects
In principle, files saved with Python pickle
are comparatively compact. Nevertheless, it is also possible and, in some cases, advisable to compress the memory files even more. This works, for example, with the free compression program bzip2, which is part of the programming language’s standard library. In the following example, we create a string, serialize it and then apply the compression program:
Security instructions for working with Python pickle
Even though Python pickle
is a practical and effective method for converting objects, there is one major drawback that you should be aware of when working with the module, which is that there is the possibility of transferring malicious code via serialized data. Although this is not a problem with your own data, you have to be careful when it comes to third-party files. Therefore, only ever deserialize memory files when you know and trust the source!
Deploy directly via GitHub: With Deploy Now from IONOS, you not only benefit from automatic framework detection and a quick setup, but you can also choose between a variety of plans. Find the solution that perfectly suits your needs!