Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

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.

import pickle

Serialize a dict object into pickle.

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

Deserialize the dict from the file.

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