如何在python3.6中将两个json文件合并为一个

时间:2019-04-22 09:03:26

标签: json python-3.6

在python3.6中将两个json文件合并为一个

我尝试了data1.update(data2),但没有用

import json

with open("test.json") as fin1:
    data1 = json.load(fin1)
with open("test_userz.json") as fin2:
    data2 = json.load(fin2)

data1.update(data2)
with open("merged.json", "w") as fout:
    json.dump(data1, fout)

1 个答案:

答案 0 :(得分:0)

您可以像这样合并

>>> data1=json.loads('{"test1":"one"}')
>>> data2=json.loads('{"test2":"two"}')
>>> data3=[]
>>> data3.append(data1)
>>> data3.append(data2)
>>> json.dumps(data3)
'[{"test1": "one"}, {"test2": "two"}]'