【问题标题】:Convert unicode codepoint to UTF8 hex in python在python中将unicode代码点转换为UTF8 hex
【发布时间】:2009-05-15 10:13:24
【问题描述】:

我想将从文件中读取的多个 unicode 代码点转换为它们的 UTF8 编码。

例如,我想将字符串 'FD9B' 转换为字符串 'EFB69B'

我可以使用这样的字符串文字手动执行此操作:

u'\uFD9B'.encode('utf-8')

但我无法以编程方式解决。

【问题讨论】:

    标签: python unicode


    【解决方案1】:

    使用内置函数chr()将数字转换为字符,然后对其进行编码:

    >>> chr(int('fd9b', 16)).encode('utf-8')
    '\xef\xb6\x9b'
    

    这是字符串本身。如果您希望字符串为 ASCII 十六进制,则需要遍历并将每个字符 c 转换为十六进制,使用 hex(ord(c)) 或类似的。

    注意:如果您仍然坚持使用 Python 2,您可以改用 unichr()

    【讨论】:

    • 输出与问题所指定的不符。无论如何,如果 OP 高兴的话……
    • Py3K 仅供参考,它是chr(int('fd9b', 16)).encode('utf-8')
    • @tzot: ''.join('{:02X}'.format(n) for n in chr(int('FD9B', 16)).encode()) 在 Python 3 中给出字符串 'EFB69B'
    • 我编辑了您的答案以使用 Python 3 解决方案并添加注释以防有人仍然坚持使用 Python 2。我希望您不介意...chr(int('1f607', 16))
    【解决方案2】:

    这里有一个完整的解决方案:

    >>> ''.join(['{0:x}'.format(ord(x)) for x in unichr(int('FD9B', 16)).encode('utf-8')]).upper()
    'EFB69B'
    

    【讨论】:

      【解决方案3】:
      Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39) 
      [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
      Type "help", "copyright", "credits" or "license" for more information.
      >>> u'\uFD9B'.encode('utf-8')
      '\xef\xb6\x9b'
      >>> s = 'FD9B'
      >>> i = int(s, 16)
      >>> i
      64923
      >>> unichr(i)
      u'\ufd9b'
      >>> _.encode('utf-8')
      '\xef\xb6\x9b'
      

      【讨论】:

        【解决方案4】:
        data_from_file='\uFD9B'
        unicode(data_from_file,"unicode_escape").encode("utf8")
        

        【讨论】:

          【解决方案5】:

          如果输入字符串长度是 4 的倍数(即您的 unicode 代码点是 UCS-2 编码的),那么试试这个:

          import struct
          
          def unihex2utf8hex(arg):
              count= len(arg)//4
              uniarr= struct.unpack('!%dH' % count, arg.decode('hex'))
              return u''.join(map(unichr, uniarr)).encode('utf-8').encode('hex')
          
          >>> unihex2utf8hex('fd9b')
          'efb69b'
          

          【讨论】:

            【解决方案6】:

            因为您在使用带有宽 unicode 字符的 unichr 时可能会遇到错误:

            >>> n = int('0001f600', 16)
            >>> unichr(n)
            ValueError: unichr() arg not in range(0x10000) (narrow Python build)
            

            这是在窄 python 构建上使用宽 unicode 的另一种方法:

            >>> n = int('0001f600', 16)
            >>> s = '\\U{:0>8X}'.format(n)
            >>> s = s.decode('unicode-escape')
            >>> s.encode("utf-8")
            '\xf0\x9f\x98\x80'
            

            并使用原始问题的价值:

            >>> n = int('FD9B', 16)
            >>> s = '\\u{:0>4X}'.format(n)
            >>> s = s.decode('unicode-escape')
            >>> s.encode("utf-8")
            '\xef\xb6\x9b'
            

            【讨论】:

              猜你喜欢
              • 2012-11-01
              • 2011-10-10
              • 1970-01-01
              • 2015-10-08
              • 2011-12-05
              • 2018-09-20
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多