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 differ in typing, syntax, and speed, but both are valuable for developers. Java uses static typing and braces for blocks, while Python uses dynamic typing and indentation. Python is ge..

    Python Interview Questions

    How does exception handling in Python differ from Java?

    Python’s try-except block allows for effective exception handling, enabling programmers to catch and handle errors without ending the program. This technique ensures that errors can be managed grace..

    Python Interview Questions

    What is a module and package in Python?

    Python modules are ".py" files with classes, functions, and variables. Packages are directories containing modules and sub-packages, marked by an init.py file. This file can be empty but indicates the..