问题描述:

在使用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)

这样就变成了分行显示

在这里插入图片描述

GitHub 加速计划 / js / json
47
5
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:6 个月前 )
c67d5382 * :alembic: try matrix for latest Signed-off-by: Niels Lohmann <mail@nlohmann.me> * :recycle: refactor from https://github.com/nlohmann/json/issues/4745#issuecomment-2810128420 Signed-off-by: Niels Lohmann <mail@nlohmann.me> * :construction_worker: simplify CI Signed-off-by: Niels Lohmann <mail@nlohmann.me> * :construction_worker: simplify CI Signed-off-by: Niels Lohmann <mail@nlohmann.me> * :rotating_light: fix cpplint warning Signed-off-by: Niels Lohmann <mail@nlohmann.me> * :construction_worker: simplify CI Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me> 14 小时前
88c92e60 * :rotating_light: fix warnings Signed-off-by: Niels Lohmann <mail@nlohmann.me> * :rotating_light: fix warnings Signed-off-by: Niels Lohmann <mail@nlohmann.me> * :alembic: enable ranges support Signed-off-by: Niels Lohmann <mail@nlohmann.me> * :fire: remove ci_nvhpc job Signed-off-by: Niels Lohmann <mail@nlohmann.me> * :alembic: enable ranges support Signed-off-by: Niels Lohmann <mail@nlohmann.me> * :fire: remove ci_nvhpc job Signed-off-by: Niels Lohmann <mail@nlohmann.me> * :rotating_light: fix warning Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me> 1 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐