【问题标题】:Reading from excel sheet and writing exact characters to the json file从 excel 表中读取并将确切的字符写入 json 文件
【发布时间】:2019-01-20 23:53:15
【问题描述】:

我有一个 excel 表,我正在从中读取并将读取的值写入 json 文件。但问题是字符不是按原样写的。

例如: 如果文本是“Молба”,则用unicode或其他方式写为“\u041b\u0438\u0447\u043d\u0430”。

我用来写入文件的代码是

    with open('data.json', 'w') as file:
        str = json.dumps(json_list, indent=4)
        file.write(str)
        file.close()

json_list 有对象列表。

任何解决此问题的建议都会有所帮助。

【问题讨论】:

    标签: python json python-unicode


    【解决方案1】:

    将 ensure_ascii=False 传递给 json.dumps() 函数以执行此操作

    【讨论】:

    • 收到错误'ascii' codec can't encode character u'\u0421' in position 61: ordinal not in range(128)
    • 尝试将 encoding='utf-8' 传递给 open()
    • 我使用了json.dumps(json_list, indent=4, ensure_ascii=False).encode('utf8'),它可以正常工作:) 感谢您的宝贵时间。
    【解决方案2】:

    考虑到@leotrubach 的建议,

    json.dumps(json_list, indent=4, ensure_ascii=False).encode('utf8') 按要求工作。

    【讨论】:

      猜你喜欢
      • 2019-08-22
      • 2021-01-11
      • 2019-10-06
      • 1970-01-01
      • 1970-01-01
      • 2017-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多