問題描述
今天寫了一個python腳本,所有編碼默認utf-8,通過json.dumps轉爲json,結果使用print打印出來,打印結果的中文字符一直顯示爲\xxxxx\xxxxx
原因
找個半天終於發現了問題所在:
$ help('json')
從中發現以下說明:
| __init__(self, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, encoding='utf-8', default=None)
…………
| If *ensure_ascii* is true (the default), all non-ASCII
| characters in the output are escaped with \uXXXX sequences,
| and the results are str instances consisting of ASCII
| characters only. If ensure_ascii is False, a result may be a
| unicode instance. This usually happens if the input contains
| unicode strings or the *encoding* parameter is used.
說明json.dumps默認把所有中文字符轉爲了ascii,而我們使用的是utf-8,所以print出來中文不對
解決方法
爲調用的json.dumps方法加入ensure_ascii=False參數就OK了,如下所示
$ json.dumps({"data": data}, ensure_ascii=False, indent=2)
{
"data": [
{
"{#REALURL}": "測試_xxx"
},
{
"{#REALURL}": "測試_xxx"
},
.
.
.
]
}