python numpy保存和读取字典,一行代码读取所有npy结尾的字典
保存:
import numpy as np
data_dict = {'a':{1:'abc',2:'cde'},'b':[1,2,3,'aa']}
np.save('data.dict',data_dict) # 自动添加.npy
读取:
import numpy as np
data_dict_npy = np.load('data.dict.npy',allow_pickle=True) # 加上.npy
data_dict_a = data_dict_npy.item()['a'] # 读取key ’a‘ 的内容,切记要加.item()表示取内容!!!
print(data_dict_a)
结果如下: