我需要将json对象的列表(结果)保存到文件中,因此列表的每个元素必须保存在一行中。对象列表保存在文件( data_file )中。我正在使用以下代码:
# some code here
fi =open(final_results, "a+")
for i in range(1,20)
#some code here
with open(targetpath, 'r') as data_file:
json_data = data_file.read()
results = json.loads(json_data)
for en in results:
json.dump(en, fi)
fi.write("\n")
在收集数据的最后,fi可以包含一些5Go。 我的问题是:有没有更好的方法来完成这项工作?