Content:
Pickle is a Python utility for serialising and deserialising objects. A serialised object can be saved to disk for reading later, while deserialising a file allows the object to be loaded back into your application.
Serialised objects can also be transmitted over a network.
When using Pickle, serialising objects is referred to as ‘pickling’, while deserialising is known as ‘unpickling’. We’ll be using these terms throughout this article.
This article will show you how you can use pickle in your application.
How Pickle Works
We’ve briefly mentioned the basic function of Pickle, but here we’ll take a look at exactly what Pickle does to your data.
When an object is pickled, it’s converted to a byte-stream. This byte-stream is then saved in a pickle-specific data format. This data format is binary encoded, and is not human readable.
Unpickling turns this binary-encoded byte-stream back into a readable object, which can be used in your application.
It can be compared in functionality to JSON. It’s possible to serialise and deserialise data using JSON, though the main difference is the output format. JSON encodes data using unicode characters, and is therefore human readable.
A major advantage of Pickle compared to JSON is the ability to encode more complex Python objects.
Getting Started with Pickle
To start using Pickle, you first need to import it.
import pickle
There are four main functions in Pickle, which are outlined below.
pickle.dump()
pickle.dumps()
pickle.load()
pickle.loads()
These will appear familiar to anyone that has used JSON in Python before.
Pickling
Both dump()
and dumps()
are used to pickle your objects. The two functions are essentially the same – the difference is how the pickled object is handled.
dump()
takes your object, followed by a file name. The file name is the file which will contain the pickled object.
dumps()
instead returns a string, and does not take a file name as an input.
Unpickling
Similarly, there are two functions available for unpickling; load()
and loads()
.
load()
reads data from a file, while loads()
reads a string.
Example
Here we have an object, containing some dummy data.
import pickle
class data:
string_element = 'string element'
int_element = 10
dict_element = {
'first': 'first element',
'second': 'second element',
'third': 'third element'
}
}
data_obj = data()
We can pickle this object using dumps()
.
pickled_obj = pickle.dumps(data_obj)
The pickled object can then be sent over a network, or passed elsewhere in your application.
To unpickle, we simply pass the pickled object to loads()
.
unpickled_obj = pickle.loads(pickled_obj)
data_obj
and unpickled_obj
will contain exactly the same data.
To save data_obj to a file, we can instead use dump()
. You’ll first need to open a file buffer, and pass this to dump()
.
file = open(file.pickle, 'rb')
pickle.dump(data_obj, file)
You should now see that a file called file.pickle
has been created.
To load this data back in, use load()
.
unpickled_file = pickle.load(file)
Conclusion
Pickling is a great way to save complex objects to files in Python. This article goes through the basics, but there is a lot more functionality available. Be sure to check out our other articles to learn more about Pickle.