【问题标题】:Convert non-ascii character to ascii character将非ASCII字符转换为ASCII字符
【发布时间】:2021-04-20 10:00:02
【问题描述】:

我正在寻找转换此字符串:

test = "Il était toujours temps d’étouffer l’inflation par la hausse des taux"

到这里:

test = "Il était toujours temps d'étouffer l'inflation par la hausse des taux"

我的意思是用“真实的”单词/字母替换非 ascii 字符。

【问题讨论】:

  • 看看this
  • 我看到了主题,但我不想删除非 ascii,只是为了转换为真正的字母。我将 # -- coding: utf-8 -- 放在我的文件顶部,但它不起作用。
  • 我明白了,那你可以考虑Unidecode 为例。

标签: python


【解决方案1】:
test.encode('cp1252').decode('utf-8')

我已经尝试过了,它确实有效。我是从here获取的

【讨论】:

  • 请注意,这适用,因为文本实际上是将 UTF-8 字节错误解释为 cp1252 的结果。如果它们是其他形式的字节,或者被解释为其他错误编码,则需要更改所涉及的精确参数。
猜你喜欢
  • 1970-01-01
  • 2010-12-30
  • 2014-10-21
  • 1970-01-01
  • 1970-01-01
  • 2011-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多