attribute error:“str”对象没有属性“decode”,在我的cod的第二行出现此错误

2024-06-26 14:03:57 发布

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

这是我的密码

if __name__ == "__main__":
    key = "0123456789abcdef0123456789abcdef".decode('hex')  /this line is having error
    plain_1 = "1weqweqd"
    plain_2 = "23444444"
    plain_3 = "dddd2225"
    print(plain_1)
    print(plain_2)
    print(plain_3)

    cipher = Present(key)

输出

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

Tags: keyname密码ifismainlineerror
1条回答
网友
1楼 · 发布于 2024-06-26 14:03:57

这是因为你试图解码一个字符串。bytes类型可以解码,但不能str类型。您应该在此之前对(key.encode())进行编码(或使用b"foo"),以将字符串转换为bytes对象。

>>> foo = "adarcfdzer"
>>> type(foo)
<class 'str'>
>>> foo = foo.encode()
>>> type(foo)
<class 'bytes'>
>>> foo = foo.decode()
>>> type(foo)
<class 'str'>

相关问题 更多 >