Pickling allows you to serialize and de-serializing Python object structures. In short, Pickling is a way to convert a python object into a character stream so that this character stream contains all the information necessary to reconstruct the object in another python script.
To successfully reconstruct the object, The Pickled byte stream contains instructions to the unpicker to reconstruct the original object structure along with instruction operands, which help in populating the object structure.
There are currently 6 different protocols that can be used for Pickling. The higher the protocol used, the more recent the version of Python needed to read the Pickle produced. To know more about Pickle Protocol.
Pickle has two main methods. The first one is a dump
, which dumps an object to a file object and the second one is load
, which loads an object from a file object.
pickle.dump(object, file_obj, protocol)
This function takes three arguments -
HIGHEST_PROTOCOL
, the highest protocol version available will be used.)So let’s continue to practical:
import pickle
Below is the example of pickling and unpickling -
import pickle
def pickle_data():
data = {
'name': 'Prashant',
'profession': 'Software Engineer',
'country': 'India'
}
filename = 'PersonalInfo'
outfile = open(filename, 'wb')
pickle.dump(data,outfile)
outfile.close()
pickle_data()
To Open the file we used open
function, The first argument should be the name of your file and the second argument is wb
, wb
refers the writing in binary mode. This means that the data will be written in the form of byte objects.
import pickle
def unpickling_data():
file = open(filename,'rb')
new_data = pickle.load(file)
file.close()
return new_data
print(unpickling_data())
Here r
stands for reading mode and the b
stands for binary mode. You’ll be reading a binary file. The output of the Unpickling function is the same as we pickled.
{'name': 'Prashant', 'profession': 'Software Engineer', 'country': 'India'}
The following data types can be pickled:
The documentation of the Pickle module states:
The Pickle module is not secure against erroneous or maliciously constructed data. Never unpickle data received from an untrusted or unauthenticated source.
I hope that you now have a fair understanding of Pickling/Unpickling in python.
https://docs.python.org/3.8/library/pickle.html
https://stackoverflow.com/questions/7501947/understanding-pickling-in-python