How to deserialize Python objects from a file
How to deserialize Python objects from a file.
Here is a step-by-step tutorial on how to deserialize Python objects from a file:
Step 1: Understand Serialization and Deserialization Serialization is the process of converting an object into a format that can be stored or transmitted, such as a file or a network stream. Deserialization is the reverse process of converting the serialized data back into an object.
Step 2: Choose a Serialization Library Python provides several libraries for serialization, such as Pickle, JSON, and YAML. In this tutorial, we will focus on the Pickle library, which is a built-in serialization module in Python.
Step 3: Create Objects to Serialize
Before we can deserialize objects, we need to have some objects to serialize. Let's create a simple example class called Person with attributes like name, age, and address.
class Person:
def __init__(self, name, age, address):
self.name = name
self.age = age
self.address = address
Step 4: Serialize Objects and Save to a File
To serialize objects, we will use the pickle.dump() function from the Pickle library. This function takes two arguments: the object to serialize and a file object to write the serialized data to.
import pickle
person1 = Person("John Doe", 25, "123 Main St")
person2 = Person("Jane Smith", 30, "456 Elm St")
# Serialize objects and save to a file
with open("persons.pkl", "wb") as file:
pickle.dump(person1, file)
pickle.dump(person2, file)
In the above code, we create two Person objects and save them to a file called "persons.pkl".
Step 5: Deserialize Objects from a File
To deserialize objects, we will use the pickle.load() function from the Pickle library. This function takes a file object as an argument and returns the deserialized object.
# Deserialize objects from a file
with open("persons.pkl", "rb") as file:
deserialized_person1 = pickle.load(file)
deserialized_person2 = pickle.load(file)
# Access and print the deserialized objects
print(deserialized_person1.name)
print(deserialized_person2.age)
In the above code, we open the "persons.pkl" file in read mode and use pickle.load() to deserialize the objects. We can then access and print the attributes of the deserialized objects.
Step 6: Handle Exceptions When deserializing objects, it's important to handle exceptions that may occur. For example, if the file is not found or the file contains incompatible data.
try:
with open("persons.pkl", "rb") as file:
deserialized_person1 = pickle.load(file)
deserialized_person2 = pickle.load(file)
except FileNotFoundError:
print("File not found!")
except EOFError:
print("End of file reached!")
In the above code, we use a try-except block to handle the FileNotFoundError and EOFError exceptions that may occur during deserialization.
That's it! You now know how to deserialize Python objects from a file using the Pickle library. Remember to handle exceptions appropriately and ensure the compatibility of serialized and deserialized objects.