多行 base64 字符串 returns 解码后仅最后一行 Python 3
Multi line base64 string returns only last line after decoding in Python 3
我有一组 base64 编码的字符串。当我尝试对其进行解码时,解码后的文本只有实际字符串的最后一行。在任何在线 base64 解码器上,都会显示整个文本。在解码之前,我尝试过将文本编码和不编码为 utf-8。
这是我的代码:
import base64
encodedStr = "QXJyaXZhbCBNZXNzYWdlDURhdGUgKFVUQyk6IDI2SlVMMjAN"
#with encoding
encodedstr_bytes = encodedStr.encode('utf-8')
decodedBytes = base64.b64decode(encodedstr_bytes)
decodedStr = decodedBytes.decode('utf8')
print(decodedStr)
#without encoding
decodedBytes = base64.b64decode(encodedStr)
decodedStr = decodedBytes.decode("utf-8")
print(decodedStr)
Output :
Date (UTC): 26JUL20
Date (UTC): 26JUL20
Required Output :
Arrival Message
Date (UTC): 26JUL20
您的字符串有回车符 return(“\r”),但没有换行符(“\n”)。在 Windows 上,这指示打印机 return 到行的开头并覆盖那里的内容。以下代码的行为相同:
print("foo\rbar") # Prints "bar"
print("fooqux\rbar") # Prints "barqux"
我有一组 base64 编码的字符串。当我尝试对其进行解码时,解码后的文本只有实际字符串的最后一行。在任何在线 base64 解码器上,都会显示整个文本。在解码之前,我尝试过将文本编码和不编码为 utf-8。 这是我的代码:
import base64
encodedStr = "QXJyaXZhbCBNZXNzYWdlDURhdGUgKFVUQyk6IDI2SlVMMjAN"
#with encoding
encodedstr_bytes = encodedStr.encode('utf-8')
decodedBytes = base64.b64decode(encodedstr_bytes)
decodedStr = decodedBytes.decode('utf8')
print(decodedStr)
#without encoding
decodedBytes = base64.b64decode(encodedStr)
decodedStr = decodedBytes.decode("utf-8")
print(decodedStr)
Output :
Date (UTC): 26JUL20
Date (UTC): 26JUL20
Required Output :
Arrival Message
Date (UTC): 26JUL20
您的字符串有回车符 return(“\r”),但没有换行符(“\n”)。在 Windows 上,这指示打印机 return 到行的开头并覆盖那里的内容。以下代码的行为相同:
print("foo\rbar") # Prints "bar"
print("fooqux\rbar") # Prints "barqux"