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文件樣式很美觀且中文正確顯示
相關文章
相關標籤/搜索