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\')
- codecs python官方文档中文翻译 使用给定模式打开编码文件,并返回提供透明编码/解码的打包版本。默认文件模式为“r”,表示以读取模式打开文件。
- 使用
codecs.open(encoding=\'foo\')需要明确的知道Option.json文件的编码格式 - 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\')
- python 3 中可以直接使用open打开文件并且指定编码格式
Option.json
{
"default": "中文",
"field": "_display_name",
"type": "str",
"len": "255",
"not_null": "True"
}