10-3. Serializing Objects – The pickle Module
The pickle module in Python is used for serializing and deserializing Python objects. Serialization, also known as pickling, is the process of converting a Python object into a byte stream, which can be stored in a file or transmitted over a network. Deserialization, or unpickling, is the reverse process where the byte stream is converted back into a Python object.
Steps to Use the pickle Module for Serializing Objects
- Import the pickle Module: You need to import the pickle module to use its functions.
import pickle
- Open a File for Binary Writing: You need to open a file in binary mode for writing (wb). This file will store the serialized byte stream.
with open('data.pkl', 'wb') as file:
# Perform pickling operations here
- Pickle the Object and Write it to the File: Use the pickle.dump method to serialize the object and write it to the specified file.
with open('data.pkl', 'wb') as file:
pickle.dump(my_object, file)
Here, my_object can be any Python object, such as a dictionary or a set.
- Close the File: If you are using the with statement, the file will be automatically closed. Otherwise, you should explicitly close the file.
file.close()
Example: Serializing a Dictionary and a Set
Here's a complete example that demonstrates how to pickle a dictionary and a set:
import pickle
# Example dictionary and set
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
my_set = {'apple', 'banana', 'cherry'}
# Open a file for binary writing
with open('data.pkl', 'wb') as file:
# Pickle the dictionary
pickle.dump(my_dict, file)
# Pickle the set
pickle.dump(my_set, file)
Steps to Deserialize the Objects
- Open the File for Binary Reading: Open the file in binary mode for reading (rb).
with open('data.pkl', 'rb') as file:
# Perform unpickling operations here
- Unpickle the Objects: Use the pickle.load method to deserialize the objects from the file.
with open('data.pkl', 'rb') as file:
loaded_dict = pickle.load(file)
loaded_set = pickle.load(file)
Example: Deserializing the Dictionary and Set
This example code shows how to unpickle a dictionary and a set:
import pickle
# Open the file for binary reading
with open('data.pkl', 'rb') as file:
# Unpickle the dictionary
loaded_dict = pickle.load(file)
# Unpickle the set
loaded_set = pickle.load(file)
# Display the loaded objects
print(loaded_dict)
print(loaded_set)
Putting it all together
import pickle
def main():
# Example dictionary and set
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
my_set = {'apple', 'banana', 'cherry'}
# Open a file for binary writing
with open('data.pkl', 'wb') as file:
# Pickle the dictionary
pickle.dump(my_dict, file)
# Pickle the set
pickle.dump(my_set, file)
# Open the file for binary reading
with open('data.pkl', 'rb') as file:
# Unpickle the dictionary
loaded_dict = pickle.load(file)
# Unpickle the set
loaded_set = pickle.load(file)
# Display the loaded objects
print(loaded_dict)
print(loaded_set)
if __name__=="__main__":
main()
Output:
{'name': 'Alice', 'age': 30, 'city': 'New York'}
{'banana', 'cherry', 'apple'}