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文件样式很美观且中文正确显示
 

猜你喜欢

转载自www.cnblogs.com/wang-mengmeng/p/12584840.html
今日推荐