Python - 将多个Pickle对象加载到单个字典中

时间:2015-06-16 10:15:18

标签: python serialization pickle

所以我的问题是这个...我有多个Pickle目标文件(这是Pickled Dictionaries),我想加载它们,但基本上将每个字典合并为一个更大的字典。

E.g。

我有pickle_file1和pickle_file2都包含字典。我想将pickle_file1和pickle_file2的内容加载到my_dict_final中。

修改 根据要求,这是我到目前为止:

for pkl_file in pkl_file_list:
    pickle_in = open(pkl_file,'rb')
    my_dict = pickle.load(pickle_in)
    pickle_in.close()

本质上,它可以工作,但只是覆盖my_dict的内容而不是附加每个pickle对象。

提前感谢您的帮助。

3 个答案:

答案 0 :(得分:4)

my_dict_final = {}  # Create an empty dictionary
with open('pickle_file1', 'rb') as f:
    my_dict_final.update(pickle.load(f))   # Update contents of file1 to the dictionary
with open('pickle_file2', 'rb') as f:
    my_dict_final.update(pickle.load(f))   # Update contents of file2 to the dictionary
print my_dict_final

答案 1 :(得分:0)

您可以使用dict.update功能。

pickle_dict1 = pickle.load(picke_file1)
pickle_dict2 = pickle.load(picke_file2)
my_dict_final = pickle_dict1
my_dict_final.update(pickle_dict2)

Python Standard Library Docs

答案 2 :(得分:0)

@ Nunchux,@ Vikas Ojha如果字典恰巧具有公共密钥,那么update方法将覆盖那些公共密钥的值。示例:

>>> dict1 = {'a': 4, 'b': 3, 'c': 0, 'd': 4}
>>> dict2 = {'a': 1, 'b': 8, 'c': 5}

>>> All_dict = {}                   
>>> All_dict.update(dict1)          
>>> All_dict.update(dict2)          

>>> All_dict                        
{'a': 1, 'b': 8, 'c': 5, 'd': 4}

如果您想避免这种情况并继续增加公用键的数量,一种选择是使用以下策略。应用于您的示例,这是一个最小的工作示例:

import os 
import pickle
from collections import Counter 

dict1 = {'a': 4, 'b': 3, 'c': 0, 'd': 4}
dict2 = {'a': 1, 'b': 8, 'c': 5}

# just creating two pickle files: 
pickle_out = open("dict1.pickle", "wb") 
pickle.dump(dict1, pickle_out) 
pickle_out.close() 

pickle_out = open("dict2.pickle", "wb")  
pickle.dump(dict2, pickle_out) 
pickle_out.close()  

# Here comes: 
pkl_file_list = ["dict1.pickle", "dict2.pickle"]

All_dict = Counter({})  
for pkl_file in pkl_file_list:  
    if os.path.exists(pkl_file):  
        pickle_in = open(pkl_file, "rb")  
        dict_i = pickle.load(pickle_in)  
        All_dict = All_dict + Counter(dict_i)  

print (dict(All_dict))

这将很高兴为您提供:

{'a': 5, 'b': 11, 'd': 4, 'c': 5}