我想将数组和字典写入文件(可能更多),然后能够稍后读取文件并从该文件重新创建数组和字典。在Python中是否有合理的方法可以做到这一点?
我建议你使用shelve(随python一起提供)。例如:
import shelve
d = shelve.open('file.txt') # in this file you will save your variables
d['mylist'] = [1, 2, 'a'] # thats all, but note the name for later.
d['mydict'] = {'a':1, 'b':2}
d.close()
读取值:
import shelve
d = shelve.open('file.txt')
my_list = d['mylist'] # the list is read from disk
my_dict = d['mydict'] # the dict is read from disk
如果您要保存numpy数组,那么我建议您使用针对此用例优化的joblib。
Pickle 将是实现它的一种方法(它在标准库中)。
import pickle
my_dict = {'a':1, 'b':2}
# write to file
pickle.dump(my_dict, open('./my_dict.pkl', 'wb'))
#load from file
my_dict = pickle.load(open('./my_dict.pkl', 'rb'))
对于数组,您可以在 numpy 中使用 ndarray.dump()
方法,这对于大型数组更有效。
import numpy as np
my_ary = np.array([[1,2], [3,4]])
my_ary.dump( open('./my_ary.pkl', 'wb'))
但是您当然也可以将所有内容放入同一个泡菜文件中或使用搁置(使用泡菜),就像另一个答案中建议的那样。
pickle 格式模糊了数据和代码之间的界限,我不喜欢使用它,除非我是相关数据的唯一编写者和读者,并且我确定它没有被篡改。
如果您的数据结构只是非序列类型、字典和列表,则可以使用 json
模块将其序列化为json
。这是一种纯数据格式,可以可靠地读回。它不处理元组,而是将它们视为列表。
下面是一个示例。
a = [1,2,3,4]
b = dict(lang="python", author="Guido")
import json
with open("data.dump", "r") as f:
x, y = json.load(f)
print x # => [1, 2, 3, 4]
print y # => {u'lang': u'python', u'author': u'Guido'}
它并非完全不变,但通常足够好。