python2 'str' object has no attribute 'decode'
上述代碼,報(bào)錯(cuò):
'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:
>>> 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')?
轉(zhuǎn)載于:https://www.cnblogs.com/davidwang456/p/8963978.html
總結(jié)
以上是生活随笔為你收集整理的python2 'str' object has no attribute 'decode'的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 密码控件安全技术浅析及攻击实例
- 下一篇: pyDes 实现 Python 版的 D