Ben Chuanlong Du's Blog

And let it direct your passion with reason.

Serialize and Deserialize Object Using Pickle in Python

Tips and Traps

  1. Make sure to use the mode rb/wb when read/write pickle files.
In [1]:
import pickle

Serialize a dict object into pickle.

In [4]:
with open("out.pp", "wb") as fout:
    pickle.dump({"x": 1}, fout)

Deserialize the dict from the file.

In [10]:
with open("out.pp", "rb") as fin:
    dic = pickle.load(fin)
dic
Out[10]:
{'x': 1}

Comments