Explain pickling and unpickling in Python ?

devquora
devquora

Posted On: Feb 22, 2018

 

In Python Pickling is used for serializing objects in binary streams. Pickling can be performed on objects of data types like Booleans, Integers, Floats, Complex numbers, normal and Unicode Strings, Tuples, Lists, Sets, and Dictionaries.

UnPickling is reverse of the Pickling process, here binary streams have deserialized an object.

A simple example of Pickling and UnPickling In Python

import pickle

// Pickling
dogs_dict = { 'Ozzy': 3, 'Filou': 8, 'Luna': 5, 'Skippy': 10, 'Barco': 12, 'Balou': 9, 'Laika': 16 }
filename = 'dogs'
outfile = open(filename,'wb')
pickle.dump(dogs_dict,outfile)
outfile.close()
// UnPickling
infile = open(filename,'rb')
new_dict = pickle.load(infile)
infile.close()

    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    Python Interview Questions

     How is Python different from Java?

    Java and Python are way different from each other, but both of them can be useful tools for high-tech developers. Also, ..

    Python Interview Questions

    How does exception handling in Python differ from Java?

    Python uses its own techniques to implement exception handling. <try-except> is the block that can be utilized by..

    Python Interview Questions

    What is a module and package in Python?

    Modules can be defined as the Python files with an extension “.py”. The module name will be same as that of the fil..