问题描述:

在使用python 向 json文件写入内容时,写入的内容一行显示,如图
在这里插入图片描述
看着就很难受


分析以及解决方案:

开始是这样写的:结果就是一行

new_dict = {"version": "4.2.10", "flags": {},"shapes":[0],"imagePath": "IMG_8816.jpg", "imageData": "null", "imageHeight": 1440, "imageWidth": 1920}
with open("xx.json", 'w') as f1:
	json.dump(new_dict, f1)

后来改为 文件写入的方式

new_dict = {"version": "4.2.10", "flags": {},"shapes":[0],"imagePath": "IMG_8816.jpg", "imageData": "null", "imageHeight": 1440, "imageWidth": 1920}
data = json.dumps(new_dict, indent=1)
print(data)
with open("xx.json", 'w', newline='\n') as f:
 	f.write(data)

这样就变成了分行显示

在这里插入图片描述

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐