python-数据写入json文件时如何保证格式美观且中文正常显示?

import json
from config.cfg import *


def write_data(data, filename):
    file_path = os.path.join(config_path, filename)
    # print(file_path)
    with open(file_path, 'w', encoding='utf8') as f:  # 如果json文件不存在会自动创建
        json.dump(data, f,indent=4,ensure_ascii=False)  # 红色部分就是控制格式和中文
        print("数据写入json文件完成...")
ensure_ascii=False : 写入中文

上述代码生成的json文件样式很美观且中文正确显示

 

 

 
posted @ 2020-03-27 22:37  爱打盹的猫猫  阅读(2292)  评论(1编辑  收藏  举报