pickle
module provides serializing and de-serializing functionality for python objects. Serializing also referred to as pickling
, marshaling
, flattening
. Pickling is byte serialization whereas JSON is text serialization.
Serialized data generated by pickle is python-specific
. Hence other languages won't be able to de-serialize pickled data by python pickle.
Warning: This module is not secure against malicious unstructured data. Never unpickle data from untrusted sources.
import pickle
pickle.dump(obj,file,protocol=None,fix_imports=True)
- It writes serialized/pickled version of obj
to file
object. Default protocol is 3 and if negative number is given for protocol then it selects HIGHEST_PROTOCOL
. If fix_imports
is True
then it maps new Python 3
module names to Python 2
to avoid failure during read by Python 2
.pickle.load(file,fix_imports=True,encoding="ASCII",errors="strict")
- It reads data from file
object. fix_imports
has same use as above method. encoding
can be bytes
to read 8-bit string as byte objects, latin1
to read numpy array, datetime, date and time instances.Note: File should be opened in binary mode to write pickled byte stream data to it. File should have write()
and read()
methods to be used by dump()
and load()
methods.
with open('pickle_data.dat', 'wb') as f:
pickle.dump('Writing String data to file', f)
pickle.dump({'Key':'Value'},f)
pickle.dump([1,9,4,7,3,5],f)
with open('pickle_data.dat','rb') as f:
unpickled_string = pickle.load(f)
unpickled_dict = pickle.load(f)
unpickled_list = pickle.load(f)
print(unpickled_string)
print(unpickled_dict)
print(unpickled_list)
dumps(obj,protocol=None,fix_imports=True)
- Same as dump()
method but instead of writing pickled data to file, it returns it.loads(bytes_obj,fix_imports=True,encoding='ASCII',errors='strict')
- Reads data from bytes object and returns in original format. Other parameters have same meaning as load()
.pickled_string = pickle.dumps('Generating pickled text data')
print(type(pickled_string),pickled_string)
pickled_dict = pickle.dumps({'some_key':'some_value'})
print(type(pickled_dict),pickled_dict)
pickled_list = pickle.dumps([4,6,8,9,10])
print(type(pickled_list),pickled_list)
unpickled_text = pickle.loads(pickle.dumps('Generating pickled text data'))
print(unpickled_text, type(unpickled_text))
unpickled_dict = pickle.loads(pickle.dumps({'some_key':'some_value'}))
print(unpickled_dict, type(unpickled_dict))
unpickled_range = pickle.loads(pickle.dumps(range(10)))
print(unpickled_range, type(unpickled_range))
unpickled_list = pickle.loads(pickle.dumps([10,9,8,7,6,5,4,3,2,1]))
print(unpickled_list, type(unpickled_list))
pickle.HIGHEST_PROTOCOL
- Highest protocol availablepickle.DEFAULT_PROTOCOL
- Default protocol is version 3 currently.pickle.PickleError
- Common base class for other Pickling Exceptions. Extends Exception
class.pickle.PicklingError
- Raised when an unpickable object is encountered by a module. Extends PickleError
.pickle.UnpicklingError
- Raised when unpickling fails due to corrupt data or security violations. Extends PickleError
.If you are more comfortable learning through video tutorials then we would recommend that you subscribe to our YouTube channel.
When going through coding examples, it's quite common to have doubts and errors.
If you have doubts about some code examples or are stuck somewhere when trying our code, send us an email at coderzcolumn07@gmail.com. We'll help you or point you in the direction where you can find a solution to your problem.
You can even send us a mail if you are trying something new and need guidance regarding coding. We'll try to respond as soon as possible.
If you want to