Load and Dump in Python
The Pickle module provides the load() and dump() methods in Python. The pickle module in Python is used for serializing (pickling) and deserializing (unpickling) Python objects. Python's read () and write() functions only support text parameters, so they cannot directly access binary files. Data conversion is necessary while reading and writing.
The load and dump methods are used with serialization and deserialization. In these terms, the load reads data from a file or serialized format and reconstructs the data into an object. The dump transforms an object into a serialized format or writes it into a file.
Serialization and deserialization tasks are frequently performed in Python using the pickle package. It has the dump() and load() APIs for saving and loading objects.
The pickle module lets you transform complicated elements into byte streams that may be saved to files, sent across networks, or kept in databases. The original object can then be retrieved by unpickling the byte stream.
Functions of Pickle Module
- Pickle.dump(obj, file[, protocol]): Using this method, the object gets serialized and written to the file object. The protocol parameter, which can be any integer between 0 and 5, defines the pickling protocol. The best protocol is used if none are specified.
- Pickle.dumps(obj[, protocol]): Instead of sending the object to a file, this function serializes the object and returns the serialized form as a bytes object. It is helpful to send pickled data across a network or save it in memory.
- Pickle.load(file): This function reconstructs an object after reading a pickled object from the file object. By deserializing the byte stream, the object is rebuilt.
- Pickle.loads(bytes_object): It reconstructs the object from a bytes object that contains the pickled representation of the object. It is helpful when you wish to unpickle an item from memory or a network transfer immediately.
Example for Dump() Method in Python
import pickle
data = {
'name': 'John',
'age': 30,
'City': 'New York'
}
#Dumping the data to a file
with open('data.pkl', 'wb') as file:
pickle.dump(data, file)
Explanation:
In the example above, we have a data dictionary defined by the user that contains some information. The data dictionary is serialized and saved into a file called data.pkl using the dump() method. The file is opened in binary mode ('wb') to ensure platform compatibility.
When this code is executed, a file called data.pkl will be generated that contains the data dictionary's serialized representation.
Example for Load() Method in Python
import pickle
# Load the data from the file
with open('data.pkl', 'rb') as file:
loaded_data = pickle.load(file)
print(loaded_data)
Explanation:
In the above code, we need to open data.pkl file in binary mode(‘rb’) for exact file handling without any errors. The data kept in the file is then deserialized using the pickle.load() method. The loaded_data variable holds the final object.
To verify that the deserialization succeeded, we print the loaded_data at the end, showing the original dictionary.
Example code:
import pickle
# Example data
data = {
'name': 'John',
'age': 30,
'City': 'New York '
}
#Dumping the data to a file
with open('data.pkl', 'wb') as file:
pickle.dump(data, file)
# Load the data from the file
with open('data.pkl', 'rb') as file:
loaded_data=pickle.load(file)
# Print the loaded data
print(loaded_data)
Output:
![Load and Dump in Python](https://static.tutorialandexample.com/python/load-and-dump-in-python1.png)
Explanation:
In the following example, we need to import the pickle module to use load() and dump() methods. We need to create a dictionary named data to represent some information. The data dictionary is then serialized using the dump() method and sent to a file called data.pkl. The file is opened in binary mode ('wb') to ensure platform connectivity.
The data from the data.pkl file is then deserialized using the load() method. We do a binary mode ('rb') file open before passing the file to pickle.load(). The loaded_data variable holds the outcome.
We used the load() method to correctly load the data from the file and reassemble the original dictionary.
NOTE: The pickle module is primarily designed to serialize and deserialize Python objects inside the same Python environment or across compatible Python versions. It might not be appropriate for moving data across Python versions or between computer languages.
Furthermore, be careful when working with incorrect or sometimes harmful data since Pickle can run arbitrary code during deserialization. Only unpickle information from reliable sources.