【问题标题】:Python Latin-1 Supplement to decimalPython Latin-1 对小数的补充
【发布时间】:2018-01-31 14:04:47
【问题描述】:

用python

ord('a')

给予

 97

但是

ord('é')

报错

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: ord() expected a character, but string of length 2 found

如何获取除 ASCII (é = 233) 以外的其他字母的小数点,例如 https://en.wikipedia.org/wiki/List_of_Unicode_characters#Latin-1_Supplement

【问题讨论】:

  • 你用的是什么python版本?我在 python 2 和 3 中尝试了同样的方法,都运行良好。
  • 我在 Python 2.7.9 中遇到了同样的问题,但在 Python 3.4.2 中运行良好
  • 为了重现,您需要一个以 UTF-8 编码但缺少 encoding: utf-8 注释的文件。我认为这只会影响 Python 2。

标签: python character-encoding decimal iso-8859-1


【解决方案1】:

你可以先解码:

ord('é'.decode('utf-8'))  # outputs 233

【讨论】:

    猜你喜欢
    • 2013-06-11
    • 1970-01-01
    • 2016-09-15
    • 2023-04-05
    • 1970-01-01
    • 2010-09-21
    • 2012-02-11
    • 2015-04-22
    • 1970-01-01
    相关资源
    最近更新 更多