您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python2 'str' object has no attribute 'decode'

5b51 2022/1/14 8:23:57 python 字数 2770 阅读 624 来源 www.jb51.cc/python

'0102030405060708'.decode('hex')上述代码,报错:'str' object has no attribute 'decode'查找原因:https://stackoverflow.com/questions/29030725/str-object-has-no-attribute-decodeYou cannot decode string objects; they are?already?decoded. You'll have to use a different method.You can use the?codecs.decode()?function?to apply?hex?as a codec:>>> import codecs>>> codecs.decode('ab', 'hex')b'xab'This applies a?Binary transform?codec; it is the equivalent of using the?base64.b16decode()?function, with the input string converted to uppercase:>>> import base64>>> base64.b16decode('AB')b'xab'You can also use the?binascii.unhexlify()?function?to 'decode' a sequence of hex digits to bytes:>>> import binascii>>> binascii.unhexlify('ab')b'xab'Either way, you'll get a?bytes?object.?使用第二种方式解决了:>>> import base64>>> base64.b16decode('AB')

概述

<div class="cnblogs_code">

.decode()

上述代码,报错:

'str' object has no attribute 'decode'

查找原因:

https://stackoverflow.com/questions/29030725/str-object-has-no-attribute-decode

You cannot decode string objects; they are already decoded. You'll have to use a different method.

You can use the codecs.decode() function to apply hex as a codec:

>>>>

This applies a @L_403_1@ codec; it is the equivalent of using the base64.b16decode() function,with the input string converted to uppercase:

>>>>

You can also use the binascii.unhexlify() function to 'decode' a sequence of hex digits to bytes:

>>>>

Either way,you'll get a bytes object.

使用第二种方式解决了:


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶