AttributeError:“str”对象没有“decode”Python3属性

2024-04-27 02:17:31 发布

您现在位置:Python中文网/ 问答频道 /正文

该行在python2.7.6中工作得很好,但在python3.3.5中失败。如何在Python 3中解码为hex值。

return x.replace(' ', '').replace('\n', '').decode('hex')

回溯

AttributeError: 'str' object has no attribute 'decode'

Tags: noreturnobjectattribute解码replaceattributeerrorhas
1条回答
网友
1楼 · 发布于 2024-04-27 02:17:31

要将十六进制转换为字符串,请使用^{}

>>> from binascii import unhexlify
>>> unhexlify(x.replace(' ', '').replace('\n', ''))

但是,首先需要将x转换成bytes才能使其工作,对于Python 3。执行以下操作:

>>> x = x.encode('ascii', 'strict')

然后进行十六进制到字符串的转换。

相关问题 更多 >