Python 在每个文件合并后添加打断行

Python 在每个文件合并后添加打断行,python,Python,我有很多JSON文件,如下所示: 例如 1.json {"name": "one", "description": "testDescription...", "comment": ""} {"name": "test", "description": "testDescription...", "comment": ""} {"name": "two", "description": "testDescription...", "comment": ""} ... {"name": "

我有很多JSON文件,如下所示:

例如

1.json

{"name": "one", "description": "testDescription...", "comment": ""}
{"name": "test", "description": "testDescription...", "comment": ""}
{"name": "two", "description": "testDescription...", "comment": ""}

...
{"name": "one", "description": "testDescription...", "comment": ""},
{"name": "test", "description": "testDescription...", "comment": ""},
{"name": "two", "description": "testDescription...", "comment": ""}
{"name": "one", "description": "testDescription...", "comment": ""},
{"name": "test", "description": "testDescription...", "comment": ""},
{"name": "two", "description": "testDescription...", "comment": ""}
test.json

{"name": "one", "description": "testDescription...", "comment": ""}
{"name": "test", "description": "testDescription...", "comment": ""}
{"name": "two", "description": "testDescription...", "comment": ""}

...
{"name": "one", "description": "testDescription...", "comment": ""},
{"name": "test", "description": "testDescription...", "comment": ""},
{"name": "two", "description": "testDescription...", "comment": ""}
{"name": "one", "description": "testDescription...", "comment": ""},
{"name": "test", "description": "testDescription...", "comment": ""},
{"name": "two", "description": "testDescription...", "comment": ""}
two.json

{"name": "one", "description": "testDescription...", "comment": ""}
{"name": "test", "description": "testDescription...", "comment": ""}
{"name": "two", "description": "testDescription...", "comment": ""}

...
{"name": "one", "description": "testDescription...", "comment": ""},
{"name": "test", "description": "testDescription...", "comment": ""},
{"name": "two", "description": "testDescription...", "comment": ""}
{"name": "one", "description": "testDescription...", "comment": ""},
{"name": "test", "description": "testDescription...", "comment": ""},
{"name": "two", "description": "testDescription...", "comment": ""}
我想将它们合并到一个JSON文件中,如:

merge_json.json

{"name": "one", "description": "testDescription...", "comment": ""}
{"name": "test", "description": "testDescription...", "comment": ""}
{"name": "two", "description": "testDescription...", "comment": ""}

...
{"name": "one", "description": "testDescription...", "comment": ""},
{"name": "test", "description": "testDescription...", "comment": ""},
{"name": "two", "description": "testDescription...", "comment": ""}
{"name": "one", "description": "testDescription...", "comment": ""},
{"name": "test", "description": "testDescription...", "comment": ""},
{"name": "two", "description": "testDescription...", "comment": ""}
我有以下代码:

import json
import glob

result = []
for f in glob.glob("*.json"):
    with open(f, "r") as infile:
        try:
            result.append(json.load(infile))
        except ValueError as e:
            print(f,e)
result = '\n'.join(result)

with open("merged.json", "w", encoding="utf8") as outfile:
    json.dump(result, outfile)
我可以合并所有文件,但所有文件都在一行中。添加每个文件后,如何添加换行符:

而不是:

{"name": "one", "description": "testDescription...", "comment": ""},{"name": "test", "description": "testDescription...", "comment": ""},{"name": "two", "description": "testDescription...", "comment": ""}
让他们像:

merge_json.json

{"name": "one", "description": "testDescription...", "comment": ""}
{"name": "test", "description": "testDescription...", "comment": ""}
{"name": "two", "description": "testDescription...", "comment": ""}

...
{"name": "one", "description": "testDescription...", "comment": ""},
{"name": "test", "description": "testDescription...", "comment": ""},
{"name": "two", "description": "testDescription...", "comment": ""}
{"name": "one", "description": "testDescription...", "comment": ""},
{"name": "test", "description": "testDescription...", "comment": ""},
{"name": "two", "description": "testDescription...", "comment": ""}

非常感谢您的帮助。

可能需要漂亮的打印:您必须计算出自己的参数的精确组合outfile的可能副本。write(result)infle.read()可能也足以在不解析json的情况下完成工作,只需将其写回文件即可
result = []
for f in glob.glob("*.json"):
    with open(f, "r") as infile:
        try:
            result.append(json.load(infile))
        except ValueError as e:
            print(f,e)

with open("merged.json", "a", encoding="utf8") as outfile:
    for i in result:
        json.dump(i, outfile)
        outfile.write('\n')