CodeGym /Java Course /Python SELF EN /Working with pickle

Working with pickle

Python SELF EN
Level 22 , Lesson 1
Available

9.1 Introduction to pickle

The pickle module lets you save pretty much any Python object to a file or string and then bring it back to its original state. pickle supports lots of data types, including custom class objects, and automates the process of converting objects to a byte format and back.

Main functions of the pickle module

  • Reading and writing an object to a file
    • pickle.dump(obj, file): Serializes the obj object and writes it to the open file.
    • pickle.load(file): Reads from the file and deserializes the object.
  • Reading and writing an object to a byte array
    • pickle.dumps(obj): Serializes the obj object and returns it as a byte object.
    • pickle.loads(bytes): Deserializes the object from the byte object.

Let's go over them in a bit more detail below:

9.2 Serialization to a file

Main steps of serialization to a file:

  1. Open the file in byte writing mode (wb).
  2. Serialize the object using pickle.dump(obj, file).
  3. To deserialize the object use pickle.load(file).
  4. Close the file.

Serialization and deserialization of an object to a file

Basically, pickle doesn't care what you're serializing. For example, here's what serializing a list looks like:


import pickle

data = [1, 2, 3, 4, 5]
            
# Serialize the list to a file
with open('list.pkl', 'wb') as file:
    pickle.dump(data, file)
            
# Deserialize the list from the file
with open('list.pkl', 'rb') as file:
    loaded_data = pickle.load(file)
            
print(loaded_data)  # Output: [1, 2, 3, 4, 5]

And here's what serializing a dictionary object looks like:


import pickle

# Example object for serialization
data = {'name': 'Alice', 'age': 30, 'is_student': False}
            
# Serialize the object to a file
with open('data.pkl', 'wb') as file:
    pickle.dump(data, file)
            
# Deserialize the object from the file
with open('data.pkl', 'rb') as file:
    loaded_data = pickle.load(file)
            
print(loaded_data)  # Output: {'name': 'Alice', 'age': 30, 'is_student': False}

Want to save an object? Call dump(), want to load it? Call load(). It's that simple.

9.3 Serialization to a string

Really often, you'll need to send objects over a network, so you'll need to save the object to a string instead of a file. For this, pickle has the dumps and loads methods with an extra s at the end.

Main actions for serialization and deserialization to a string (or byte array):

  • Serialize the object using pickle.dumps(obj).
  • To deserialize the object use pickle.loads(data).

Serialization and deserialization of an object to a string

Serializing an object to a string (or byte set) is even simpler — you just need to call one method — dumps().

Example:


import pickle

# Example object for serialization
data = {'name': 'Bob', 'age': 25, 'is_student': True}
            
# Serialize the object to a string
serialized_data = pickle.dumps(data)
print(serialized_data)
            
# Deserialize the object from the string
loaded_data = pickle.loads(serialized_data)
print(loaded_data)  # Output: {'name': 'Bob', 'age': 25, 'is_student': True}

9.4 Errors during serialization

Sometimes errors pop up during serialization. In that case, the pickle module will throw exceptions:

  • pickle.PicklingError — error during serialization.
  • pickle.UnpicklingError — error during deserialization.

Examples:


import pickle

data = {'key': 'value'}
            
try:
    # Serialize the object to a file
    with open('data.pkl', 'wb') as file:
        pickle.dump(data, file)
except pickle.PicklingError as e:
    print(f"Serialization error: {e}")
            
try:
    # Deserialize the object from the file
    with open('data.pkl', 'rb') as file:
        loaded_data = pickle.load(file)
    print(loaded_data)
except pickle.UnpicklingError as e:
    print(f"Deserialization error: {e}")

Usually, as a programmer, there's nothing you can do about a serialization/deserialization error during the program's runtime. All you can do is log it so you can figure out why it happened later.

The main cause of these errors is a change in the object's format. You save an object to a file, then your program gets an update, and the object gets new fields or loses old ones. And then the old saved object is trying to load into the class of the new object...

Again, this situation often occurs when working with networks — a program version 2.18 sends an object that's a bit different from what a program version 3.1 expects. This is called a migration problem, and it's something often faced in long-lived and popular projects.

2
Task
Python SELF EN, level 22, lesson 1
Locked
Serialize a list to a file
Serialize a list to a file
2
Task
Python SELF EN, level 22, lesson 1
Locked
Serializing a dictionary to a string
Serializing a dictionary to a string
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION