我目前正在处理大约1500个列表的JSON文件。我想将每个列表保存在单独的行中。我当前的代码以下列方式提供输出:
{"1":x ,"2":y},{"1":x ,"2":y}
但是,我想保存它们,如下所示,不带逗号,并且在单独的行中:
{"1":x ,"2":y}
{"1":x ,"2":y}
我的代码如下:
#this is appending my filtered data into results (running in loop for 1500 listings)
filtered_result.append(filtered_data)
#this is dumping results into my output file
filtered_result_json = json.dumps(filtered_result, output_file)
#this is writing results into a JSON file
output_file.write(filtered_result_json)
#closing output file
output_file.close()
答案 0 :(得分:0)
试试这个:
filtered_result.append(filtered_data)
for json_element in filtered_result:
output_file.write(json.dumps(json_element))
output_file('\n')
output_file.close()