在Python 3中,多行base64字符串仅返回解码后的最后一行

2024-09-26 22:10:01 发布

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

我有一组base64编码的字符串。当我尝试解码相同的内容时,解码的文本只有实际字符串的最后一行。在任何在线base64解码器上,都会显示整个文本。在解码之前,我尝试过将文本编码为utf-8和不编码为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

Tags: 字符串文本编码datebytes解码utfencoding
1条回答
网友
1楼 · 发布于 2024-09-26 22:10:01

字符串有回车符(“\r”),但没有换行符(“\n”)。在Windows上,这会指示打印机返回到行首并覆盖其中的内容。以下代码的行为相同:

print("foo\rbar")  # Prints "bar"
print("fooqux\rbar")  # Prints "barqux"

相关问题 更多 >

    热门问题