在Python2.x中,我能够做到:
>>> '4f6c6567'.decode('hex_codec')
'Oleg'
但在Python3.2中,我遇到了以下错误:
>>> b'4f6c6567'.decode('hex_codec')
Traceback (most recent call last):
File "<pyshell#25>", line 1, in <module>
b'4f6c6567'.decode('hex_codec')
TypeError: decoder did not return a str object (type=bytes)
根据docshex_codec
应该提供“字节到字节的映射”。所以这里正确使用了byte string的对象。
如何消除此错误,以避免从十六进制编码的文本转换时出现难以处理的工作区?
在Python 3中,使用
bytes.decode()
方法将原始字节解码为Unicode,因此必须使用codecs.getdecoder()
或codecs.decode()
从codecs
模块获取解码器,用于bytes
到-bytes
编码:文档中似乎缺少后一个函数,但它有一个有用的docstring。
您可能还想看看
binascii.unhexlify()
。相关问题 更多 >
编程相关推荐