|
|
|
|
|
|
json.dump()方法有 ensure_ascii 參數,ensure_ascii 默認是true,保證輸出所有傳入的非 ASCII 字符都已轉義。如果 ensure_ascii 為 false,這些字符將按原樣輸出。

Python將JSON數據寫入文件時處理非ASCII字符
如果要存儲非 ASCII 字符,請按原樣使用以下代碼。
import json
unicode_string = u"\u00f8"
print("unicode String is ", unicode_string)
# set ensure_ascii=False
print("JSON character encoding by setting ensure_ascii=False")
print(json.dumps(unicode_string, ensure_ascii=False))
輸出:
unicode String is ø
JSON character encoding by setting ensure_ascii=False
"ø"
你也可以參考這篇文章:Python將Unicode或非ASCII數據序列化為JSON原樣字符串。
