【问题标题】:How to write Polish character from JOSON file to JSON file? [duplicate]如何将波兰字符从 JSON 文件写入 JSON 文件? [复制]
【发布时间】:2014-10-03 22:59:27
【问题描述】:

我试过了:

# -*- 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)

【问题讨论】:

    标签: python json encoding


    【解决方案1】:

    尝试将参数 ensure_ascii 设置为 False:

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

    【讨论】:

      猜你喜欢
      • 2013-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-13
      • 1970-01-01
      • 2013-04-22
      相关资源
      最近更新 更多