可以通过在 json.dump() 函数中传递 indent 参数来设置缩进的空格数,如下所示:

import json

# 将 self.cmd_dict 字典数据写入 json 文件并缩进 2 格
with open(cmd_path, 'w') as f:
    json.dump(self.cmd_dict, f, indent=2)

在上述代码中,将 self.cmd_dict 字典数据写入 json 文件时,通过 indent 参数设置缩进的空格数为 2。

Python 中使用 json.dump() 函数缩进 JSON 数据

原文地址: https://www.cveoy.top/t/topic/ok9u 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录