我有一个巨大的json数组文件已下载,需要拆分成较小的文件,但我需要以下格式的较小文件(数组中每个新对象的换行符):(原始json也采用相同的格式)< / p>
[
{"a":"a1","b":"b1","c":"c1"},
{"a":"a2","b":"b2","c":"c2"},
{"a":"a3","b":"b3","c":"c3"}
]
我使用了json.dump,但它只是在一行中打印较小的数组,并且使用缩进选项也没有以上述格式提供输出
答案 0 :(得分:2)
虽然我不知道你原来的json是什么样的,但你基本上会想要这样的东西
lines = []
for something in original_json:
line={something['a']:something['aa']} #whatever you need to do to get your values
lines.append(line)
#alternatively you can simplify this by doing lines.append({something['a']:something['aa'], etc}
with open('myfile.json', 'a+') as f1:
f1.write("[\n")
for line in lines:
f1.write("%s,\n"%(line))
f1.write("]")