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"
}