简体   繁体   English

在 python 中保存和加载混合数据的最佳方法是什么?

[英]What is the best way to save and load mixture data in python?

I have mixture data in python, which consist of我在 python 中有混合数据,其中包括

  1. some list of dictionary and一些字典列表和
  2. some numpy array.一些 numpy 阵列。

I can save first part as json, and second part as binary npz.我可以将第一部分保存为 json,将第二部分保存为二进制 npz。 But which format should i prefer for save both of them in one file?但是我应该更喜欢哪种格式将它们都保存在一个文件中?

Do you need the saved data to be human-readable?你需要保存的数据是人类可读的吗? If not, you can try Pickle https://docs.python.org/3/library/pickle.html如果没有,你可以试试 Pickle https://docs.python.org/3/library/pickle.html

For examples please see the related question: How can I use pickle to save a dict?有关示例,请参阅相关问题: 如何使用 pickle 保存字典?

(Example code from @Blender in above link) (上面链接中@Blender的示例代码)

import pickle

a = {'hello': 'world'}

with open('filename.pickle', 'wb') as handle:
    pickle.dump(a, handle, protocol=pickle.HIGHEST_PROTOCOL)

with open('filename.pickle', 'rb') as handle:
    b = pickle.load(handle)

print a == b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM