pickle

 

2022-04-25

import:: import pickle

doc:: pickle


用法

基于内存

1
- dumps(): 保存
2
- loads(): 读取
1
import pickle
2
3
# 保存
4
num = pickle.dumps(42)
5
# 读取
6
pickle.loads(num)

基于文件

1
- dump(): 保存
2
- load(): 读取
1
import pickle
2
3
# 保存
4
with open(<file_path>, 'wb') as f:
5
pickle.dump(data, f)
6
7
# 读取
8
with open (<file_path>, 'rb') as f:
9
data = pickle.load(f)