导入pickle模块,该模块可提供简单的持久化功能,将对象以文件的形式储存在本地,可通过pickle.dump()和pickle.load()简单的序列化和反序列化储存的对象。
import pickle
dic_1 = {"a":"abc","b":5,"c":[1,"d",3]}
with open("pic.pkl","wb") as f:
pickle.dump(dic_1,f)
储存完成,需要读取时只用pickle.load()反序列化即可:
with open("pic.pkl","rb") as f:
dic_2 = pickle.load(f)
print(dic_2)
{'a': 'abc', 'b': 5, 'c': [1, 'd', 3]}
网友评论