Python Pickle: Serialize and Deserialize Object

In this tutorial, we will introduce how to serialize and deserialize python object using python pickle.

Python Pickle - Serialize and Deserialize Object for Beginners

We will introduce how to serialize python object.

1.Import library

import pickle

2.Create a python object to serialize

exampleObj = {'Python':3,'KDE':5,'Windows':10}

3.Create a file to save serialized result

fileObj = open('data.obj', 'wb')

4.Serialize python object using pickle.dump()

pickle.dump(exampleObj,fileObj)
fileObj.close()

Run this code, we will save serialized result in data.obj file.

Then, we will deserialize python object.

1.Import library and open serialized result file

import pickle   

fileObj = open('data.obj', 'rb')

2.Deserialize python object using pickle.load()

exampleObj = pickle.load(fileObj)
fileObj.close()
print(exampleObj)

Run this code, you will see:

Python Pickle - Serialize and Deserialize Object