猿问

如何从 ISO-8859-1 格式转换为 UTF-8,然后转换为十六进制格式字符串 python

如何将 UTF-8 格式字符 '戗' 转换为十六进制值并将其存储为字符串“0xe6 0x88 0xa7”。


with open(fromFilename,  encoding = "ISO-8859-1") as f:

    while True:

        c = f.read(1)

        if not c:

            print ("End of file")

            break

        print ("Read a character: %c", c)

        newC = repr(c.encode('utf-8'))

        print ("Read a decode character: %c", newC)

        newString = newString + newC

这是我的代码。请让我知道出了什么问题。


四季花海
浏览 257回答 1
1回答

千巷猫影

这在 Python 3.7 中对我有用a = '戧'encoded_bytes = a.encode(encoding='utf-8')print(' '.join([hex(b) for b in encoded_bytes]))>>> 0xe6 0x88 0xa7
随时随地看视频慕课网APP

相关分类

Python
我要回答