将5个词典合并为另存为txt文件python

将5个词典合并为另存为txt文件python,python,pandas,dictionary,merge,save,Python,Pandas,Dictionary,Merge,Save,我有以下5本字典: d1 = {'a': 1, 'b': 2} d2 = {'b': 10, 'c': 11} d3 = {'e': 13, 'f': 15} d4 = {'g': 101, 'h': 111} d5 = {'i': 10, 'j': 11} 我想合并这五本词典并另存为txt文件。输出应如下所示: {{'a': 1, 'b': 2}, {'b': 10, 'c': 11}, {'e': 13, 'f': 15}, {'g': 101, 'h': 111}, {'i': 10,

我有以下5本字典:

d1 = {'a': 1, 'b': 2}
d2 = {'b': 10, 'c': 11}
d3 = {'e': 13, 'f': 15}
d4 = {'g': 101, 'h': 111}
d5 = {'i': 10, 'j': 11}
我想合并这五本词典并另存为txt文件。输出应如下所示:

{{'a': 1, 'b': 2}, {'b': 10, 'c': 11}, {'e': 13, 'f': 15}, {'g': 101, 'h': 111}, {'i': 10, 'j': 11}}

到目前为止我试过什么

d = {**d1, **d2, **d3, **d4, **d5}
df = pd.DataFrame.from_dict(d, orient='index')
df.to_csv('output.txt')

这不会正确合并和保存输出。如何实现这一点?

您不需要熊猫来处理文件(至少对于这个问题)

正确合并您的词典。 要将此数据保存为txt文件,您只需要python文件处理:

with open('file.txt', 'w') as file:
    # first convert dictionary to string
    file.write(str(d))
file.txt的内容:

{'a': 1, 'b': 10, 'c': 11, 'e': 13, 'f': 15, 'g': 101, 'h': 111, 'i': 10, 'j': 11}
检查
json.dump(d,file)
也许你写的是('b'),而不是('d'),因为你说它保存不正确。
with open('file.txt', 'w') as file:
    # first convert dictionary to string
    file.write(str(d))
{'a': 1, 'b': 10, 'c': 11, 'e': 13, 'f': 15, 'g': 101, 'h': 111, 'i': 10, 'j': 11}