Featured Post
How to Understand Pickling and Unpickling in Python
- Get link
- Other Apps
Pickle and Unpickle
The process of writing the state of an object to the file (converting a class object into a byte stream) and storing it in the file is called pickling. It is also called object serialization.
The process of reading the state of an object from the file ( converting a byte stream back into a class object) is called unpickling. It is an inverse operation of pickling. It is also called object deserialization. The pickling and unpickling can implement by using a pickling module since binary files support byte streams. Pickling and unpickling should be possible using binary files.
Data types you can pickle
- Integers
- Booleans
- Complex numbers
- Floats
- Normal and Unicode strings
- Tuple
- List
- Set and dictionaries which contains pickling objects
- Classes and built-in functions can define at the top level of a module.
Functions you need
dump()
The above function performs pickling. It returns the pickled representation of an object as a byte object instead of writing it to the file. It is called to serialize an object hierarchy.
Syntax:
import pickle
pickle.dump(object, file, protocol)
where
the object is a python object to serialize
a file is a file object in which the serialized python object will be stored
protocol if not specified is 0. If specified as HIGHEST PROTOCOL or negative, then the highest protocol version available will be used.
load()
The above function performs unpickling. It reads a pickled object from a binary file and returns it as an object. It is used to deserialize a data stream.
Syntax:
import pickle
pickle.load(file)
Related
- Get link
- Other Apps
Comments
Post a Comment
Thanks for your message. We will get back you.