Serializing Python Objects using Pickle
Pickle is the native data serialization module for Python.1 It is the simplest and most effective way for storing and sharing Python data in the short-term.
Data Serialization
It is the process of converting structured data into a format through which storing and sharing of the data is possible. Without serialization, the data would be force converted into a str, where the
The serialized data can be recovered to its original structure.
Some of the other common serializing methods are:
Flat data | Nested data |
---|---|
repr | YAML |
csv | JSON |
NumPy Array | XML |
Code
1
2
3
4
5
6
import pickle
with open('file.pkl', 'rb') as f: # The 'b' stands for byte.
data = pickle.load(f)
with open('file.pkl', 'wb') as f:
pickle.dump(data, f)
Resources
This post is licensed under CC BY 4.0 by the author.