Python Pickle Example
Pickling without a File
In this example, we will serialize the dictionary data and store it in a byte stream. Then this data is deserialized using pickle.loads() function back into the original Python object.
Python3
import pickle # initializing data to be stored in db Omkar = { 'key' : 'Omkar' , 'name' : 'Omkar Pathak' , 'age' : 21 , 'pay' : 40000 } Jagdish = { 'key' : 'Jagdish' , 'name' : 'Jagdish Pathak' , 'age' : 50 , 'pay' : 50000 } # database db = {} db[ 'Omkar' ] = Omkar db[ 'Jagdish' ] = Jagdish # For storing # type(b) gives <class 'bytes'>; b = pickle.dumps(db) # For loading myEntry = pickle.loads(b) print (myEntry) |
Output:
{'Omkar': {'key': 'Omkar', 'name': 'Omkar Pathak', 'age': 21, 'pay': 40000},
'Jagdish': {'key': 'Jagdish', 'name': 'Jagdish Pathak', 'age': 50, 'pay': 50000}}
Pickling with a File
In this example, we will use a pickle file to first write the data in it using the pickle.dump() function. Then using the pickle.load() function, we will load the pickle fine in Python script and print its data in the form of a Python dictionary.
Python3
# Python3 program to illustrate store # efficiently using pickle module # Module translates an in-memory Python object # into a serialized byte stream—a string of # bytes that can be written to any file-like object. import pickle def storeData(): # initializing data to be stored in db Omkar = { 'key' : 'Omkar' , 'name' : 'Omkar Pathak' , 'age' : 21 , 'pay' : 40000 } Jagdish = { 'key' : 'Jagdish' , 'name' : 'Jagdish Pathak' , 'age' : 50 , 'pay' : 50000 } # database db = {} db[ 'Omkar' ] = Omkar db[ 'Jagdish' ] = Jagdish # Its important to use binary mode dbfile = open ( 'examplePickle' , 'ab' ) # source, destination pickle.dump(db, dbfile) dbfile.close() def loadData(): # for reading also binary mode is important dbfile = open ( 'examplePickle' , 'rb' ) db = pickle.load(dbfile) for keys in db: print (keys, '=>' , db[keys]) dbfile.close() if __name__ = = '__main__' : storeData() loadData() |
Output:
Omkar => {'key': 'Omkar', 'name': 'Omkar Pathak', 'age': 21, 'pay': 40000}
Jagdish => {'key': 'Jagdish', 'name': 'Jagdish Pathak', 'age': 50, 'pay': 50000}
Advantages of Using Pickle in Python
- Recursive objects (objects containing references to themselves): Pickle keeps track of the objects it has already serialized, so later references to the same object won’t be serialized again. (The marshal module breaks for this.)
- Object sharing (references to the same object in different places): This is similar to self-referencing objects. Pickle stores the object once, and ensures that all other references point to the master copy. Shared objects remain shared, which can be very important for mutable objects.
- User-defined classes and their instances: Marshal does not support these at all, but Pickle can save and restore class instances transparently. The class definition must be importable and live in the same module as when the object was stored.
Disadvatages of Using Pickle in Python
- Python Version Dependency: Data of picle is so sensitive to the version of Python that produced. Pickled object created with one version of Python that might not be unpickled with a various versions.
- Non-Readble: The format of pickle is binary and not easily readable or editable by humans. The contracts that are in JSON or XML format can be easily modified.
- Large data inefficiency: Large datasets can slow down the pickling and unpickling. Serialization might be more appropriate for such use-cases.
Understanding Python Pickling with example
In Python, we sometimes need to save the object on the disk for later use. This can be done by using Python pickle. In this article, we will learn about pickles in Python along with a few examples.
Contact Us