Get unicode code point of a character using Python

后端 未结 5 1236
余生分开走
余生分开走 2020-12-04 20:54

In Python API, is there a way to extract the unicode code point of a single character?

Edit: In case it matters, I\'m using Python 2.7.

5条回答
  •  醉话见心
    2020-12-04 21:28

    Usually, you just do ord(character) to find the code point of a character. For completeness though, wide characters in the Unicode Supplementary Multilingual Plane are represented as surrogate pairs (i.e. two code units) in narrow Python builds, so in that case I often needed to do this small work-around:

    def get_wide_ordinal(char):
        if len(char) != 2:
            return ord(char)
        return 0x10000 + (ord(char[0]) - 0xD800) * 0x400 + (ord(char[1]) - 0xDC00)
    

    This is rare in most applications though, so normally just use ord().

提交回复
热议问题