将 JSON LINES 文件转换为 JSON 格式?

Convert JSON LINES file to JSON format?

我有一个文件(大约 6 GB),每一行都是 JSON。

{"name":"name1", "age":40, "car":null}
{"name":"name2", "age":30, "car":null}
{"name":"name3", "age":30, "car":null}

如何将它转换为 JSON 数组 Python?

加载每一行并将其保存到列表中。

with open('file.txt', 'r') as in_file:
    lines = [json.loads(line) for line in in_file.readlines()]

然后您可以将其保存到文件中,例如 json.dump()

with open('out.json', 'w') as out_file:
    json.dump(lines, out_file)
import fileinput
for line in fileinput.input("test.txt", inplace=True):
    if 1 != fileinput.filelineno():
        print(',{}'.format(line), end='')
    else:
        print('[{}'.format(line), end='')
open("test.txt","a").write(']')