Hard disks store data in the form of bits and bytes. Similarly, data is sent over the network in the form of bits and bytes. Therefore, in order to store programming language objects on a disk or send them over a network, you’ll naturally need to convert them into bits and bytes.
The process of converting data into bits and bytes is called serialization. The process of reading serialized data and converting it to object format is called deserialization. In Python, pickling and unpickling are used to serialize and deserialize objects.
What is Pickling and Unpickling
Pickling is a process of serializing objects in Python. Unpickling is the reverse process. With unpickling, you can convert serialized data back to Python objects. You can pickle and unpickle data in Python using the Python Pickle module. In this tutorial, you’ll see how to pickle and unpickle data using this module.
Pickling and Unpickling with Pickle module
The Python Pickle module contains methods that can be used to pickle and unpickle objects in Python. Depending on the destination of the serialized data, the Pickle module provides the dump()
and dumps()
methods for pickling. For unpickling, the load
and loads()
methods are used. Let’s see these four methods in detail with the help of a couple examples.
Pickling via the dump() Method
The dump()
method in Python is used to pickle objects and store them on a disk. The dump()
method takes two parameters. The first parameter is the object to be pickled and the second parameter is the file path that stores the pickled object.
In the following example, we create a list of some famous car manufacturers. Next, a file is opened with dump()
method which pickles the object.
import pickle
car_list = ["Honda", "Toyota", "Kia", "Mercedes", "Ford", "BMW"]
car_pickle = open ("E:/Datasets/car_pickle_file", "wb")
pickle.dump(car_list, car_pickle)
To unpickle a pickled object stored on a disk, you need to use the load()
method. However, before that let’s first try to read the contents of the pickled file like a normal file using the open()
method.
car_contents = open("E:/Datasets/car_pickle_file", "rb").read()
print(car_contents)
The following output confirms that the pickled object has stored data in the form of bytes.
Output:
b'\x80\x03]q\x00(X\x05\x00\x00\x00Hondaq\x01X\x06\x00\x00\x00Toyotaq\x02X\x03\x00\x00\x00Kiaq\x03X\x08\x00\x00\x00Mercedesq\x04X\x04\x00\x00\x00Fordq\x05X\x03\x00\x00\x00BMWq\x06e.'
Unpickling via the load() Method
As we said earlier, the load()
method can be used to unpickle the pickled Python object. You have to first open the pickled file using load()
method, as shown below. The load()
method unpickles the data and returns the actual object. The unpickled object is printed on the console.
car_pickle = open ("E:/Datasets/car_pickle_file", "rb")
car_contents = pickle.load(car_pickle)
print(car_contents)
In the output below, you can see contents of the actual
Output:
['Honda', 'Toyota', 'Kia', 'Mercedes', 'Ford', 'BMW']
Code More, Distract Less: Support Our Ad-Free Site
You might have noticed we removed ads from our site - we hope this enhances your learning experience. To help sustain this, please take a look at our Python Developer Kit and our comprehensive cheat sheets. Each purchase directly supports this site, ensuring we can continue to offer you quality, distraction-free tutorials.
Pickling via the dumps() Method
The dumps()
method also pickles the data. However, instead of writing the pickled object to disk, the dumps()
method returns a string that contains serialized or pickled object contents. Here’s an example.
import pickle
car_list = ["Honda", "Toyota", "Kia", "Mercedes", "Ford", "BMW"]
car_list = pickle.dumps(car_list)
print(car_list)
Output:
In the script above, the
b'\x80\x03]q\x00(X\x05\x00\x00\x00Hondaq\x01X\x06\x00\x00\x00Toyotaq\x02X\x03\x00\x00\x00Kiaq\x03X\x08\x00\x00\x00Mercedesq\x04X\x04\x00\x00\x00Fordq\x05X\x03\x00\x00\x00BMWq\x06e.'
Unpickling via the loads() Method
You can use the loads()
method to unpickle an object that is pickled in the form of a string using the dumps()
method, instead of being stored on a disk via the the dump()
method.
In the following example the loads()
method.
import pickle
car_list = pickle.loads(car_list)
print(car_list)
In the output below, you can see the unpickled
Output:
['Honda', 'Toyota', 'Kia', 'Mercedez', 'Ford', 'BMW']
Code More, Distract Less: Support Our Ad-Free Site
You might have noticed we removed ads from our site - we hope this enhances your learning experience. To help sustain this, please take a look at our Python Developer Kit and our comprehensive cheat sheets. Each purchase directly supports this site, ensuring we can continue to offer you quality, distraction-free tutorials.