Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在一个pickle文件中使用两个词典_Python_Python 2.7_Dictionary_Pickle - Fatal编程技术网

Python 在一个pickle文件中使用两个词典

Python 在一个pickle文件中使用两个词典,python,python-2.7,dictionary,pickle,Python,Python 2.7,Dictionary,Pickle,我想说得很清楚,所以我会从我知道的案例开始,这样每个人都知道我的意思。当我在一个pickle文件中只存储一个字典时,这就是我使用的方法,它非常有效 #open pickle file with open("a_pickle_file", 'rb') as input: x = pickle.load(input) #Now update the dict in your pickle file a_dict.update(x) 假设对该词典进行了任意操作,现在我按如下方式保存: #Sav

我想说得很清楚,所以我会从我知道的案例开始,这样每个人都知道我的意思。当我在一个pickle文件中只存储一个字典时,这就是我使用的方法,它非常有效

#open pickle file
with open("a_pickle_file", 'rb') as input:
  x = pickle.load(input)
#Now update the dict in your pickle file 
a_dict.update(x)
假设对该词典进行了任意操作,现在我按如下方式保存:

#Save whatever u added to the dict
pickle.dump(open(a_dict,"a_pickle_file"))
现在继续讨论我不确定的案例,从我的发现来看,似乎几乎没有文档。我想做和上面一样的事情,只是这次我将在一个列表中存储两个字典,然后像这样将列表存储在pickle文件中

#Two dict
MyFirstDict = { 1: 'a' }
MySecondDict = { 2:'b' }

#Store them in a list
two_dict = [MyFirstDict, MySecondDict]

#save them 
pkl.dump( two_dict, open( "two_dict_pkl_file", "wb" ) )
现在我在一个列表中有两个字典,它存储在我的pickle文件中。从这里开始,如何加载这个pickle文件进行操作?一旦它被加载,我如何访问里面的每个字典进行更新和操作。最后,我可以使用上面相同的
pkl.dump
语句重新保存它吗。谢谢

编辑

因此,除了需要使用以前的信息更新词典的部分之外,在大多数情况下,执行此操作的过程通常是相同的。这是一个pkl文件中两个dict列表的输出:

[{'I': 'D', 'x': 'k', 'k': [datetime.time(11, 52, 3, 514000)]}, {'I': 'D', 'x': 'k', 'D': [datetime.time(11, 52, 3, 514000)]}]

正如您所看到的,它存储得很奇怪,我似乎无法正确地分别访问每个dict来更新它们的语法。要正确地执行此操作,必须使用一些语法。

请尝试一下!执行pickle.load(),您将看到一个包含两个dict的列表。您可以通过多种方式引用它们,但这是一种常见的方法:

MyFirstDict, MySecondDict = pickle.load(open("two_dict_pkl_file", "rb"))
是的,如果要覆盖现有内容,您可以只pickle.dump它们

编辑

下面是一个说明该概念的脚本:

import pickle, datetime

test_file = "two_dict_pkl_file"

# store two dicts in a list
MyFirstDict = { 1: 'a' }
MySecondDict = { 2:'b' }
two_dict = [MyFirstDict, MySecondDict]

print "first pickle test"
print "this python list gets pickled:"
print two_dict
print
print "this is the pickle file"
pickle.dump( two_dict, open( test_file, "wb" ) )
print open(test_file, "rb").read()
print

print "update pickle test"
pickle_list = pickle.load(open(test_file, "rb"))
print "this python object is read back:"
print pickle_list
print
d1, d2 = pickle_list
two_dict = [d1, d2]
d1['time'] = d2['time'] = datetime.datetime.now().time()
pickle.dump( two_dict, open( test_file, "wb" ) )
print "this is the updated pickle:"
print open(test_file, "rb").read()
print

print "and this is the updated python:"
print pickle.load(open(test_file, "rb"))
……及其产出

first pickle test
this python list gets pickled:
[{1: 'a'}, {2: 'b'}]

this is the pickle file
(lp0
(dp1
I1
S'a'
p2
sa(dp3
I2
S'b'
p4
sa.

update pickle test
this python object is read back:
[{1: 'a'}, {2: 'b'}]

this is the updated pickle:
(lp0
(dp1
I1
S'a'
p2
sS'time'
p3
cdatetime
time
p4
(S'\n2;\x02s\x95'
p5
tp6
Rp7
sa(dp8
I2
S'b'
p9
sg3
g7
sa.

and this is the updated python:
[{1: 'a', 'time': datetime.time(10, 50, 59, 160661)}, {2: 'b', 'time': datetime.time(10, 50, 59, 160661)}]

所以我接受了你的建议,只是尝试了一下,除了更新部分之外,我把一切都弄清楚了。这里的情况与只存储一个字典不同,请参见编辑输出。查看编辑,这是python源代码,而不是pickle文件。如果这是print pickle.load(…)的结果,那么这正是您想要的-列表中有两个dict。我正在添加一个测试脚本以获取更多详细信息。我现在正在尝试,但是为什么您的最后一行中没有更新的信息呢?我的印象是我必须使用
.update
来更新字典。糟糕的是,我添加了打印语句,并意外地删除了重新分配双U列表的行。我提供了一个edit…,dict.update()仅在您想用另一个dict的内容更新一个dict时使用。如果您只想更改单个条目,
mydict[somekey]=someval
就足够了。