【问题标题】:Working with two dictionaries in one pickle file在一个 pickle 文件中使用两个字典
【发布时间】:2014-08-14 23:09:53
【问题描述】:

我想非常清楚地说明这一点,所以我从我知道的案例开始,这样每个人都知道我的意思。当我在 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 文件中。现在从这里如何加载这个泡菜文件进行操作?加载后,我如何访问其中的每个字典以进行更新和操作。最后,我可以使用上面相同的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 以更新它们必须是某种语法才能正确执行此操作。

【问题讨论】:

    标签: python python-2.7 dictionary pickle


    【解决方案1】:

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

    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(...) 的结果,那么这正是您想要的 - 列表中的两个 dicts。我正在添加一个测试脚本以获取更多详细信息。
    • 我现在试一试,但为什么你的最后一行没有更新信息?我的印象是我必须使用.update 来更新字典。
    • 我的错,我在添加打印语句时不小心删除了重新分配 two_list 的行。我提供了一个编辑。
    • ...并且 dict.update() 仅在您想用另一个字典的内容更新字典时使用。如果您只想更改单个条目,mydict[somekey] = someval 就足够了。
    猜你喜欢
    • 1970-01-01
    • 2019-10-14
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-23
    • 1970-01-01
    相关资源
    最近更新 更多