python3 保存字典到文件,方便读取
个人觉得numpy挺好用
1.使用numpy
保存为.npy文件(后缀名不可更改)
numpy中文文档:https://www.numpy.org.cn/
import numpy as np
# Save
dictionary = {'hello':'world'}
np.save('my_file.npy', dictionary)
# Load
read_dictionary = np.load('my_file.npy').item()
print(read_dictionary['hello']) # displays "world"
2.使用pickle
保存为.pkl文件(后缀名不可更改)
import pickle
def save_obj(obj, name ):
with open('obj/'+ name + '.pkl', 'wb') as f:
pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)
def load_obj(name ):
with open('obj/' + name + '.pkl', 'rb') as f:
return pickle.load(f)