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 theobj
object and writes it to the openfile
. -
pickle.load(file)
: Reads from thefile
and deserializes the object.
-
-
Reading and writing an object to a byte array
-
pickle.dumps(obj)
: Serializes theobj
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:
-
Open the file in byte writing mode
(wb)
. -
Serialize the object using
pickle.dump(obj, file)
. -
To deserialize the object use
pickle.load(file)
. - 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.
GO TO FULL VERSION