Python get character code in different encoding?

后端 未结 3 1628
情书的邮戳
情书的邮戳 2021-02-04 06:05

Given a character code as integer number in one encoding, how can you get the character code in, say, utf-8 and again as integer?

3条回答
  •  一个人的身影
    2021-02-04 06:42

    UTF-8 is a variable-length encoding, so I'll assume you really meant "Unicode code point". Use chr() to convert the character code to a character, decode it, and use ord() to get the code point.

    >>> ord(chr(145).decode('koi8-r'))
    9618
    

提交回复
热议问题