如何将JSON文件中的波兰字符写入JSON文件?

时间:2014-08-11 07:21:15

标签: python json encoding

我试过了:

# -*- coding: utf-8 -*-
with open("File1", "w") as outfile:
    json.dump( {"Tytuł":"tą"}, outfile, indent = True, encoding="utf-8")

这给了我:

{
"Tytu\u0142": "t\u0105"
}

我想得到的是:

{
"Tytuł": "tą"
}

ensure_ascii=False有效,但当我尝试从文件中获取JSON数据时,我得到UnicodeEncodeError: 'ascii' codec can't encode characters...

我使用的代码是:

json_data=open('File0')
data = json.load(json_data)
with open("File1", "w") as outfile:
    json.dump( data,outfile,indent = True,ensure_ascii=False)

1 个答案:

答案 0 :(得分:1)

尝试将参数ensure_ascii设置为False:

with open("File1", "w") as outfile:
    json.dump( {"Tytuł":"tą"}, outfile, indent = True, encoding="utf-8", ensure_ascii=False)