我无法使用 python 解码编码文本
I can't decode encoded text using python
from base64 import b64decode
uio = input("Please enter the text you want to decode...")
pu = b64decode(uio.decode())
print("Decode text : ")
print(pu)
它告诉我这个:-
pu = b64decode(uio.decode())
AttributeError: 'str' object has no attribute 'decode'
感谢任何帮助...
您正在尝试 decode()
一个字符串对象。那是行不通的。
只需删除它,它就不会再引发错误,如下所示:
pu = b64decode(uio)
如果您不确定总是有一个字符串对象,请执行此操作:
try:
pu = b64decode(uio.decode('utf-8'))
except AttributeError:
pu = b64decode(uio)
如果您热衷于性能,请注意,如果 try
中的语句经常成功,则首选 try/except
语句。如果没有,要么使用 if/else
语句替换它,要么简单地交换 try/except
中的语句——但要确保它捕获了特定的异常。
另请注意,在 python3 中,b64decode()
采用 byte-like
对象,而不是字符串。因此,上面的代码必须是这样的:
try:
pu = b64decode(uio.encode('utf-8')) # Note the use of encode()
except AttributeError:
pu = b64decode(uio)
在Python3中,所有字符串都是unicode。因此,不需要解码。 (另外,无论如何你都应该指定一个编码:))。示例:
pu = b64decode(uio.decode("utf-8"))
from base64 import b64decode
uio = input("Please enter the text you want to decode...")
pu = b64decode(uio.decode())
print("Decode text : ")
print(pu)
它告诉我这个:-
pu = b64decode(uio.decode())
AttributeError: 'str' object has no attribute 'decode'
感谢任何帮助...
您正在尝试 decode()
一个字符串对象。那是行不通的。
只需删除它,它就不会再引发错误,如下所示:
pu = b64decode(uio)
如果您不确定总是有一个字符串对象,请执行此操作:
try:
pu = b64decode(uio.decode('utf-8'))
except AttributeError:
pu = b64decode(uio)
如果您热衷于性能,请注意,如果 try
中的语句经常成功,则首选 try/except
语句。如果没有,要么使用 if/else
语句替换它,要么简单地交换 try/except
中的语句——但要确保它捕获了特定的异常。
另请注意,在 python3 中,b64decode()
采用 byte-like
对象,而不是字符串。因此,上面的代码必须是这样的:
try:
pu = b64decode(uio.encode('utf-8')) # Note the use of encode()
except AttributeError:
pu = b64decode(uio)
在Python3中,所有字符串都是unicode。因此,不需要解码。 (另外,无论如何你都应该指定一个编码:))。示例:
pu = b64decode(uio.decode("utf-8"))