解决python 保存json到文件时 中文显示16进制编码的问题

python 2.7

import codecs
import json
with codecs.open('Options.json', 'w', encoding='utf-8') as f:
    json.dump(_data, f, ensure_ascii=False, indent=4, encoding='utf-8')
  1. codecs python官方文档中文翻译 使用给定模式打开编码文件,并返回提供透明编码/解码的打包版本。默认文件模式为“r”,表示以读取模式打开文件。
  2. 使用codecs.open(encoding='foo')需要明确的知道Option.json文件的编码格式
  3. indent=4 缩进 4个空格

python 3

import json
with open('Option.json', 'w', encoding='utf-8') as f:
    json.dump(data, f , ensure_ascii=False, indent=4, encoding='utf-8')
  1. python 3 中可以直接使用open打开文件并且指定编码格式

Option.json

{
"default": "中文",
"field": "_display_name",
"type": "str",
"len": "255",
"not_null": "True"
}

猜你喜欢

转载自www.cnblogs.com/randomlee/p/11345896.html
今日推荐