【问题标题】:How to convert or decode the Unicode characters in pandas DataFrame?如何转换或解码 pandas DataFrame 中的 Unicode 字符?
【发布时间】:2017-12-26 03:00:35
【问题描述】:

我正在使用 pandas 从一个 excel 文件中读取一些数据,并进行了一些行遍历来制作一个 python 字典,然后我将它放入一个 json 文件中。

问题是我在 json 文件本身中获取了 Unicode 字符:

"C V M College of Fine Arts,\u00a0 Vallabh Vidyanagar"

如上所示,我不想要 Unicode '\u00a0',而是想要在我的 json 文件中解码它的字符表示。

在从 pandas 本身读取 excel 文件时,有什么方法可以进行这种转换(或过滤或其他任何方法)?或者在使用json.dump() 写入 json 文件时有什么方法可以做到这一点?

【问题讨论】:

  • 那个字符是一个“不间断空格”。 wiki.

标签: python excel pandas unicode data-analysis


【解决方案1】:

使用json.dumps(..., ensure_ascii=False):

foo = "C V M College of Fine Arts,\u00a0 Vallabh Vidyanagar"

import json
print(json.dumps({'foo':foo}, ensure_ascii=False))

返回:

{"foo": "C V M College of Fine Arts,  Vallabh Vidyanagar"}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    • 2015-12-09
    • 1970-01-01
    • 2019-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多