Python

How to pickle objects in Python

Any data can be serialized and deserialized in Python by using JSON and Pickle module. Before storing any data in a file, Python objects are serialized using pickle module. Python objects are converted into character streams by using this module. When the user wants to retrieve the data of the file for using another python script then the data of the file is deserialized by pickle module. The features of pickle module and how this module can be used in python script for serialization and deserialization are described in this tutorial.

Pickle Features:

  • It is mainly designed to use for Python script.
  • It is used for saving the python objects between the processes.
  • It keeps track of all serialized objects and the object that is serialized before will not serialize again.
  • It can save and restore class instance transparently.
  • It is not secure to use. So, it is not better to unpickle data from an unknown source.

dump() for serialization:

dump() function is used to convert the object data into a character stream before saving in a file. This function can take three arguments. The first two arguments are mandatory and the last argument is optional. The first argument takes a data object that needs to serialize. The second argument takes the file handler object of that file where the pckled data will be stored. The last argument takes the protocol name.

Syntax:

dump(data_object, file_object, [protocol])

load() for deserialization:

load() function is used to convert character stream data from the file into a Python object. This function contains only one argument and the file handler object of file passes as the argument value from where the data will be retrieved.

Syntax:

load(file_object)

Pickle a simple Object to store in a file

Create a file named pickle1.py with the following python script.  In the following script, a data object named dataObject is declared to store five language names by iterating the for loop. Next, open() method is used to assign a file handler for creating a binary file named languages. dump() function is used here to serialize the data of dataObject and store it in the languages file.  If the serialization will be done properly then a message, “Data is serialized” will print.

# Import the pickle module
import pickle

# Declare the object to store data
dataObject = []

# Iterate the for loop for 5 times and take language names
for n in range(5):
  raw = input('Enter a language name  :')

dataObject.append(raw)

# Open a file for writing data
file_handler = open('languages', 'wb')

# Dump the data of the object into the file
pickle.dump(dataObject, file_handler)

# close the file handler to release the resources
file_handler.close()

# Print message
print('Data is serialized')

Output:

After running the script, it will take five language names as input.

Unpickle data from a file

unpickling the data is the opposite of pickling data. Create a file named pickle2.py with the following python script. Here, open() method is used to open the binary file named languages, created in the previous example. load() function is used to unpickle the data from the file and store it in the variable dataObject. Next, for loop is used iterate the data from the dataObject and print in the terminal.

# Import the pickle module
import pickle

# Open a file handler for reading a file from where the data will load
file_handler = open('languages', 'rb')

# Load the data from the file after deserialization
dataObject = pickle.load(file_handler)

# Close the file handler
file_handler.close()

# Print message
print('Data after deserialization')

# Iterate the loop to print the data after deserialization
for val in dataObject:
  print('The data value : ', val)

Output:

The following output will appear after running the script.

Pickle a Class Object to a file

How a class object can be pickled is shown in the following example. Create a file named pickle3.py with the following script. Here, Employee class is declared to assign three data values of an employee. Next, a file handler object named fileHandler is created to open a file for writing. After initializing the class object, data are serialized using dump() function and stored in the file named employeeData. If the file will create properly then the message, “Data is serialized” will print.

# Import pickle module
import pickle

# Declare the employee class to store the value
class Employee:
  def __init__(self, name, email, post):
    self.name = name
    self.email = email
    self.post = post
 
#Create employee  object
empObject = Employee('Farheen', '[email protected]', 'Manager')

# Open file for store data
fileHandler = open('employeeData', 'wb')

# Save the data into the file
pickle.dump(empObject, fileHandler)

# Close the file
fileHandler.close()

# Print message
print('Data is serialized')

Output:

The following output will appear after running the script.

Unpickle data to a Class Object

A class with the necessary properties and methods will require to declare for retrieving the data from a file to a class object. Create a file named pickle4.py with the following code. Employee class is defined here to retrieve the data. fileObject variable is used to open the file, employeeData for reading. Next, load() function is used to store the data in the class object after deserialization. display() function of the Employee class is called to print the data values of the class object.

# Import pickle module
import pickle

# Declare employee class to read and print data from a file
class Employee:
  def __init__(self, name, email, post):
    self.name = name
    self.email = email
    self.post = post

  def display(self):
    print('Employee Information:')
    print('Name  :', self.name)
    print('Email  :', self.email)
    print('Post :', self.post)
 
# Open the file for reading
fileObject = open('employeeData', 'rb')

# Unpickle the data
employee = pickle.load(fileObject)

# Close file
fileObject.close()

#print the dataframe
employee.display()

Output:

The following output will appear after running the script.

Conclusion

Pickle module is a useful feature of python for data serialization and deserialization. After completing the examples shown in this tutorial, the data transferring from one python script to another python script will be easier for anyone.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.