Exceptions provided by the pickle module

exception pickle.PickleError 
This exception inherits Exception. It is the base class for all other exceptions raised in pickling.

 exception pickle.PicklingError 
This exception inherits PickleError. This exception is raised when an unpicklable object is encountered by Pickler. 

 exception pickle.UnpicklingError 
This exception inherits PickleError. This exception is raised when there is a problem like data corruption or a security violation while unpickling an object. 

pickle — Python object serializationThe Python Pickle Module

Python is a widely used general-purpose, high-level programming language. In this article, we will learn about pickling and unpickling in Python using the pickle module.

The Python Pickle Module

The pickle module is used for implementing binary protocols for serializing and de-serializing a Python object structure. 

  • Pickling: It is a process where a Python object hierarchy is converted into a byte stream. 
  • Unpickling: It is the inverse of the Pickling process where a byte stream is converted into an object hierarchy. 

Module Interface

  • dumps() – This function is called to serialize an object hierarchy.
  • loads() – This function is called to de-serialize a data stream.

Constants provided by the pickle module

  1. pickle.HIGHEST_PROTOCOL 
    This is an integer value representing the highest protocol version available. This is considered as the protocol value which is passed to the functions dump(), dumps(). 
  2. pickle.DEFAULT_PROTOCOL 
    This is an integer value representing the default protocol used for pickling whose value may be less than the value of the highest protocol. 

Functions provided by the pickle module
 

pickle.dump(obj, file, protocol = None, *, fix_imports = True) 
This function is equivalent to Pickler(file, protocol).dump(obj). This is used to write a pickled representation of obj to the open file object file.
The optional protocol argument is an integer that tells the pickler to use the given protocol. The supported protocols are 0 to HIGHEST_PROTOCOL. If not specified, the default is DEFAULT_PROTOCOL. If a negative number is specified, HIGHEST_PROTOCOL is selected.
If fix_imports is true and protocol is less than 3, the pickle will try to map the new Python 3 names to the old module names used in Python 2, so that the pickle data stream is readable with Python

Example :The code defines a SimpleObject class and creates instances stored in a list. It uses the pickle module to serialize these instances and writes them to an io.StringIO object.

Python3




import pickle
import io
class SimpleObject(object):
 
    def __init__(self, name):
        self.name = name
        l = list(name)
        l.reverse()
        self.name_backwards = ''.join(l)
        return
 
data = []
data.append(SimpleObject('pickle'))
data.append(SimpleObject('cPickle'))
data.append(SimpleObject('last'))
out_s = io.StringIO()
for o in data:
    print ('WRITING: %s (%s)' % (o.name, o.name_backwards))
    pickle.dump(o, out_s)
    out_s.flush()


Output : 

WRITING: pickle (elkcip)
WRITING: cPickle (elkciPc)
WRITING: last (tsal)

pickle.dumps(obj, protocol = None, *, fix_imports = True) 

This function returns the pickled representation of the object as a bytes object.

Example : This code uses the pickle' module to serialize a list containing a dictionary with various data types (string, integer, and float). It converts the data into a binary data string and prints it. This binary string represents the serialized data and can be deserialized to reconstruct the original data structure using pickle.loads().

Python3




import pickle
 
data = [ { 'a':'A', 'b':2, 'c':3.0 } ]
data_string = pickle.dumps(data)
print ('PICKLE:', data_string )


Output :  

PICKLE: (lp0
(dp1
S'a'
p2
S'A'
p3
sS'c'
p4
F3.0
sS'b'
p5
I2
sa.

pickle.load(file, *, fix_imports = True, encoding = “ASCII”, errors = “strict”) 
This function is equivalent to Unpickler(file).load(). This function is used to read a pickled object representation from the open file object file and return the reconstituted object hierarchy specified.

Example : This code defines a SimpleObject' class and creates instances stored in a list. It uses the pickle module to serialize these instances and writes them to an io.StringIO object. Then, it reads and deserializes the pickled data from in_s using pickle.load(). The code demonstrates the complete cycle of pickling and unpickling, successfully printing the information about the unpickled objects.

Python3




import pickle
import io
class SimpleObject(object):
 
    def __init__(self, name):
        self.name = name
        l = list(name)
        l.reverse()
        self.name_backwards = ''.join(l)
        return
data = []
data.append(SimpleObject('pickle'))
data.append(SimpleObject('cPickle'))
data.append(SimpleObject('last'))
out_s = io.StringIO()
for o in data:
    print ('WRITING: %s (%s)' % (o.name, o.name_backwards))
    pickle.dump(o, out_s)
    out_s.flush()
in_s = io.StringIO(out_s.getvalue())
while True:
    try:
        o = pickle.load(in_s)
    except EOFError:
        break
    else:
        print ('READ: %s (%s)' % (o.name, o.name_backwards))


Output

WRITING: pickle (elkcip)
WRITING: cPickle (elkciPc)
WRITING: last (tsal)
READ: pickle (elkcip)
READ: cPickle (elkciPc)
READ: last (tsal)

pickle.loads(bytes_object, *, fix_imports = True, encoding = “ASCII”, errors = “strict”) 
This function is used to read a pickled object representation from a bytes object and return the reconstituted object hierarchy specified.

Example : This code demonstrates pickling and unpickling data using the pickle module. It first serializes a list of dictionaries and then loads the pickled data back into another variable. It compares the original and unpickled data, showing that they are equal in content (data1 == data2) but not the same object in memory (data1 is not data2). This highlights the process of serialization and deserialization while preserving the data’s integrity.

Python3




import pickle
import pprint
data1 = [ { 'a':'A', 'b':2, 'c':3.0 } ]
print ('BEFORE:',)
pprint.pprint(data1)
 
data1_string = pickle.dumps(data1)
 
data2 = pickle.loads(data1_string)
print ('AFTER:',)
pprint.pprint(data2)
 
print ('SAME?:', (data1 is data2))
print ('EQUAL?:', (data1 == data2))


Output : 

BEFORE:[{'a': 'A', 'b': 2, 'c': 3.0}]
AFTER:[{'a': 'A', 'b': 2, 'c': 3.0}]
SAME?: False
EQUAL?: True
 

Similar Reads

Exceptions provided by the pickle module

...

Classes exported by the pickle module:

...