1.3 Pickle module in Python
v Pickle Module: -
- The pickle module in Python is used to serialize and deserialize Python objects.
- Serialization, also called pickling, is the process of converting a Python object into a byte stream so that it can be stored in a file or sent over a network.
- Deserialization, also called unpickling, is the process of converting a byte stream back into a Python object.
- The pickle module is useful for saving Python objects such as lists, dictionaries, sets, or custom objects in binary format.
- It can also be used to transfer Python data between programs, store program states, or save machine learning models for later use.
- We need to install pickle module before working upon its inbuilt methods using command prompt.
- The pickle module provides two methods: dump( ) and load( ).
1. The pickle.dump( ) function serializes a Python object and writes it to a file in binary format.
Syntax: -
Example: -
Output: -
2. The pickle.load( ) function deserializes (unpickles) a Python object from a binary file.
Syntax: -
Example: -
Output: -