【问题标题】:Type Unicode code points into Python CLI在 Python CLI 中键入 Unicode 代码点
【发布时间】:2013-12-30 16:55:39
【问题描述】:

如果已知 CP-1252 中的 'â' 字符是十六进制“E2”,那么 Python 将在 Python CLI 中输入名称“Jânis”:

>>> 'J\xe2nis'
'Jânis'

如果已知 Unicode 代码点,但不知道 CP-1252 点,如何在 Python CLI 中键入该名称? 事实上,有问题的代码点是U+00E2。另外,UTF-8 编码的字符是%C3 %A2,如果知道的话,有没有办法将它输入到 Python CLI 中?

我在 Kubuntu Linux 12.10 上使用 Python 3.2。

【问题讨论】:

  • 你的意思是这样的吗? 'J\u00e2nis'

标签: python unicode python-3.2


【解决方案1】:

使用 unicode 转义序列 (\unnnn):

>>> 'J\u00e2nis'
'Jânis'

如果您知道 utf-8,请使用bytes.decodeutf-8 是 Python 3.x 中的默认编码,因此是可选的):

>>> b'J\xc3\xa2nis'.decode('utf-8')
'Jânis'

如果您有%C3%A2,请使用urllib.parse.unquote

>>> import urllib.parse
>>> urllib.parse.unquote('J%c3%a2nis', encoding='utf-8')
'Jânis'

【讨论】:

  • 漂亮,谢谢!如果我们只有 UTF-8 代码点,您会知道该怎么做吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-20
  • 1970-01-01
相关资源
最近更新 更多