Python get character code in different encoding?

后端 未结 3 1626
情书的邮戳
情书的邮戳 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:38

    Here's an example of how the encode/decode dance works:

    >>> s = b'd\x06'             # perhaps start with bytes encoded in utf-16
    >>> map(ord, s)              # show those bytes as integers
    [100, 6]
    >>> u = s.decode('utf-16')   # turn the bytes into unicode
    >>> print u                  # show what the character looks like
    ٤
    >>> print ord(u)             # show the unicode code point as an integer
    1636
    >>> t = u.encode('utf-8')    # turn the unicode into bytes with a different encoding
    >>> map(ord, t)              # show that encoding as integers
    [217, 164]
    

    Hope this helps :-)

    If you need to construct the unicode directly from an integer, use unichr:

    >>> u = unichr(1636)
    >>> print u
    ٤
    

提交回复
热议问题