Pickle Module in Python:
|We can use the PyCharm code editor for this example. If you do not know about it then follow this link- How to install PyCharm for Python and create a program in it.|
(1) The pickle module implements a powerful algorithm for the serialization and de-serialization of a data structure implemented in Python. Pickling is the process in which the hierarchy of an object is converted into a stream of bytes. This allows an object to be transmitted and stored, and then to be rebuilt by the receiver itself retaining all the original features. You can pickle objects with the following data types i.e. booleans, integers, floats, complex numbers, (normal and Unicode) strings, tuples, lists, sets, and dictionaries. Pickle has two main methods. The first one is a dump, which dumps an object to a file object and the second one is load, which loads an object from a file object.
(2) Python Pickle Functions:
- dump()– This function is called to serialize an object hierarchy. When you write code for pickle as shown in the image, the computer.txt file is automatically created when you run the code. You can also manually create the .txt file. wb represents write binary.
As you can see that the file created by python pickle dump is a binary file and shows garbage characters in the text editor.
- load()– This function is called to de-serialize a data stream. rb represents read binary. By using the load() function we can read data from a computer.txt file in which we stored the data by using the dump() function as shown in the above image.