Skip to content

Pickling and Unpickling in Python




Python – Picking and Unpickling


As we study earlier about File Handling in python. Now we will study about a new concept i.e. Pickling and Unpickling in python and some other interesting topics like serialization and deserialization in python, pickle python , pickling in python examples, unpickle python etc. so lets start.






What is Serialization in Python ?


Serialization is a process by which we can convert an object into different formats like string or bytes so that we can transfer it over network is called serialization.




What is Deserialization in Python ?


Deserialization is a process by which we can convert different formats like string or bytes into object is called deserialization.


We can use the following techniques for serialization and deserialization in python are :

  • Pickling / Unpickling

  • JSON serialization / Deserialization

Here we will study Pickling / Unpickling first and after that we will study about JSON in the next page.





What is Pickling in Python ?


Pickling is a process by which we can convert Python objects into byte stream is called pickling.



Pickling and Unpickling in Python


What is Unpickling in Python ?


unpickling is a reverse operation, we can convert back byte stream into python object is called unpickling.


NOTE : Pickling / Unpickling is valid for Python programming only.





How to import Pickle module in Python ?


Before starting any python pickling program, we have to import pickle first.


import pickle




What is Protocol in Pickling in Python ?


Protocols means the conventions used in constructing and deconstructing Python objects to/from binary data. There are two main types of protocol are :


1. HIGHEST_PROTOCOL : It represents the highest protocol version available. It is considered as the protocol value which is passed to the functions dump(), dumps(). It always returns integer data type.


Syntax :

pickle.HIGHEST_PROTOCOL




2. DEFAULT_PROTOCOL :  It represents the default protocol used for pickling whose value may be less than the value of the highest protocol. It always returns integer data type.


Syntax :

pickle.DEFAULT_PROTOCOL




What are the functions of Pickle module in Python ?


There are four types of functions in pickle module are :

1. dump( ) : The dump( ) function means writing the object in the file.


Syntax :

pickle.dump(any python object, file stream object)

Example :

import pickle
fs = open("my_new_file.txt","wb")
List_1 = ["A", "B", "C", "D"]
pickle.dump(List_1,fs)
fs.close()


2. load( ) : The load( ) function means reading the object from the file.


Syntax :

pickle.load(file stream object)

Example :

import pickle
fs = open("my_new_file.txt","rb")
L = pickle.load(fs)
print (L)
fs.close()


3. dumps( ) : The dumps( ) function is used to serialize an python object hierarchy.


Syntax :

pickle.dumps(any python object)

Example :

import pickle
fs = open("my_new_file_1.txt","wb")
List_1 = ["A", "B", "C", "D"]
object = pickle.dumps(List_1)
fs.close()


4. loads( ) : The loads( ) function is used to de serialize the file stream object.


Syntax :

pickle.loads(bytes of object)

Example :

import pickle
fs = open("my_new_file_1.txt","rb")
L = pickle.loads(object)
print (L)
fs.close()




Pickling and Unpickling in python in same file


We already have a text file named codifyshow, and we will perform pickling and unpickling operations in the same file. We will make a very simple and easy program so that you can understand well.


CODE :

import pickle
# THIS IS PICKLING PROGRAM !
fs = open("codifyshow.txt", "wb")
List = ["Python", "Java", "C"]
DataInBytes = pickle.dumps(List)
fs.close()

# THIS IS UNPICKLING PROGRAM !
fs = open("codifyshow.txt", "rb")
L = pickle.loads(DataInBytes)
print(L)
fs.close()

Output :

['Python', 'Java', 'C']



>