多条字典写入json文件,每个字典占一行
data1=[{"asion":"0000001","name":"wuqq"},{"asion":"0000002","name":"zxw"},{"asion":"0...
data1 = [{"asion":"0000001","name":"wuqq"},{"asion":"0000002","name":"zxw"},{"asion":"0000003","name":"fengjian"}]这是一个字典list,要求就是写入json文件中的格式如下:
{"asion":"0000001","name":"wuqq"}
{"asion":"0000002","name":"zxw"}
{"asion":"0000003","name":"fengjian"}
现阶段我已经实现写入时不换行了,就是不知道怎么弄那个换行,不考虑换行代码如下:
with open('../datasets/example.json', 'w') as json_file:
for each_dict in data1:
json.dump(each_dict, json_file)
目前写入后三个字典时连着的
{"asion":"0000001","name":"wuqq"}{"asion":"0000002","name":"zxw"{"asion":"0000003","name":"fengjian"}
求大佬可以告诉小弟这个换行怎么整!!1 展开
{"asion":"0000001","name":"wuqq"}
{"asion":"0000002","name":"zxw"}
{"asion":"0000003","name":"fengjian"}
现阶段我已经实现写入时不换行了,就是不知道怎么弄那个换行,不考虑换行代码如下:
with open('../datasets/example.json', 'w') as json_file:
for each_dict in data1:
json.dump(each_dict, json_file)
目前写入后三个字典时连着的
{"asion":"0000001","name":"wuqq"}{"asion":"0000002","name":"zxw"{"asion":"0000003","name":"fengjian"}
求大佬可以告诉小弟这个换行怎么整!!1 展开
展开全部
import json
data1 = [{"asion":"0000001","name":"wuqq"},{"asion":"0000002","name":"zxw"},{"asion":"0000003","name":"fengjian"}]
with open('./example.json', 'w') as json_file:
for each_dict in data1:
json_file.write(json.dumps(each_dict)+'\n')
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询