Pickle Module for saving objects in Python

This lesson is about the Python module called pickle. I’ll tell you what is pickle and how you can use it in Python.

What is pickle

Pickle is a module responsible for serialization and deserialization of Python objects. Serialization is called pickling in Python, and deserialization is depickling.

Pickling is used to save objects such as lists, dictionaries and class objects to a stream of bytes and store them inside a file or database. Depickling is a process of reading these bytes from a stream and converting them back to objects.

When a byte stream is unpickled, Python creates an instance of the object first and populates instance with data.

When to use pickle

You can use pickles when you need to access data frequently. If you use a file, such as CSV, json, or text, the process takes much longer than loading data from a byte string.

Another situation when you can use pickle is when you send data over the network by converting objects to simpler byte stream data.

Saving a pickle to a file

Save an object to a file

The pickling process uses a module called pickle.

Let’s take a look at the following example:

First, the my_dict dictionary is created.

Next, we are going to create a BufferedWriter object by opening a file for writing in the binary mode.

With pickle.dump we are going to store the my_dict dictionary in a file called my_pickle.

When you open files for writing it’s important to close it after you finish.

Because the object saved is very small, our file size is only 37 bytes. Let’s open it with a text editor.

There are some things you can read like “test” or “111” or “year”, but the rest of the text seems like garbage. This is because the binary file is used to quickly write and read data by computer, and not human.

Loading a pickle from a file

It’s time to depickle the file and display the result. The process is very similar to pickling.

The pickled file is opened for binary reading. Next, we are going to load the file using the BuferedReader object and create a dictionary called my_dict – identical to the object saved to the file.

In the end, we are going to print the object to confirm, that indeed the object is our dictionary.