import:: import pickle
doc:: pickle
用法
基于内存
- dumps(): 保存
- loads(): 读取
import pickle
# 保存
num = pickle.dumps(42)
# 读取
pickle.loads(num)
基于文件
- dump(): 保存
- load(): 读取
import pickle
# 保存
with open(<file_path>, 'wb') as f:
pickle.dump(data, f)
# 读取
with open (<file_path>, 'rb') as f:
data = pickle.load(f)