0
4

3 回答 3

2

怎么用ensure_ascii=False

>>> d = {"descriptions": {"description16": "1 ≤ T ≤ 10"}}
>>> json.dumps(d)
'{"descriptions": {"description16": "1 \\u2264 T \\u2264 10"}}'
>>> json.dumps(d, ensure_ascii=False)
'{"descriptions": {"description16": "1 ≤ T ≤ 10"}}'
于 2018-02-16T03:33:52.610 回答
1

添加encoding='utf-8'到文件的开头。

with open('/Users/abdul/PycharmProjects/d2cApi/finalized/description_' + str(fid) + '.json', 'w', encoding="utf-8") as f:
于 2018-02-16T03:33:20.277 回答
0

我将以上两个答案结合为:

# Writing Finalized JSON description files
with open('/Users/abdul/PycharmProjects/d2cApi/finalized/description_' + str(fid) + '.json', 'w', encoding="utf-8")\
        as f:
    f.write(json.dumps(data, indent=4, ensure_ascii=False))
return json.dumps(data)

encoding="utf-8"在打开文件时添加了,并且ensure_ascii=False在将数据转储到 json 文件时也添加了。

于 2018-02-16T03:45:33.707 回答