从 Python3 中的 base64 编码字符串中删除新行“\n”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30647219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:45:11  来源:igfitidea点击:

Remove the new line "\n" from base64 encoded strings in Python3?

pythonpython-3.xbase64

提问by Mo.

I'm trying to make a HTTPS connection in Python3 and when I try to encode my username and password the base64encodebytesmethod returns the encoded value with a new line character at the end "\n" and because of this I'm getting an error when I try to connect.

我正在尝试在 Python3 中建立 HTTPS 连接,当我尝试对我的用户名和密码进行编码时,该base64encodebytes方法返回编码后的值,并在末尾添加一个换行符“\n”,因此我在以下情况下收到错误我尝试连接。

Is there a way to tell the base64library not to append a new line character when encoding or what is the best way to remove this new line character? I tried using the replacemethod but I get the following error:

有没有办法告诉base64库在编码时不要附加换行符,或者删除这个换行符的最佳方法是什么?我尝试使用该replace方法,但出现以下错误:

Traceback (most recent call last):
  File "data_consumer.py", line 33, in <module>
    auth_base64 = auth_base64.replace('\n', '')
TypeError: expected bytes, bytearray or buffer compatible object

My code:

我的代码:

auth = b'[email protected]:passWORD'
auth_base64 = base64.encodebytes(auth)
auth_base64 = auth_base64.replace('\n', '')

Any ideas? Thanks

有任何想法吗?谢谢

采纳答案by Mandar Vaze

Instead of encodestringconsider using b64encode. Later does not add \ncharacters. e.g.

而不是encodestring考虑使用b64encode. 后来不加\n字符。例如

In [11]: auth = b'[email protected]:passWORD'

In [12]: base64.encodestring(auth)
Out[12]: b'dXNlcm5hbWVAZG9tYWluLmNvbTpwYXNzV09SRA==\n'

In [13]: base64.b64encode(auth)
Out[13]: b'dXNlcm5hbWVAZG9tYWluLmNvbTpwYXNzV09SRA=='

It produces identical encoded string except the \n

它产生相同的编码字符串,除了 \n

回答by Sarit Adhikari

Following code would work

以下代码将工作

auth_base64 = auth_base64.decode('utf-8').replace('\n', '')

回答by RayLuo

I concur with Mandar's observationthat base64.xxxx_encode()would produce output without line wrap \n.

我同意Mandar 的观察,即base64.xxxx_encode()在没有 line wrap 的情况下会产生输出\n

For those who want a more confident understanding than merely an observation, these are the official promise (sort of), that I can find on this topic. The Python 3 documentationdoes mention base64.encode(...)would add newlines after every 76 bytes of output. Comparing to that, all other *_encode(...)functions do not mention their linewrap behavior at all, which can argurably be considered as "no line wrap behavior". For what it's worth, the Python 2 documentationdoes not mention anything about line wrap at all.

对于那些想要更自信的理解而不仅仅是观察的人来说,这些是我可以在这个主题上找到的官方承诺(有点)。在Python 3中的文件确实提到base64.encode(...)将每隔76个字节的输出之后添加换行符。相比之下,所有其他*_encode(...)函数根本没有提到它们的换行行为,这可以说是“无换行行为”。就其价值而言,Python 2 文档根本没有提及有关换行的任何内容。

回答by Harsh

for python3 use:-

对于 python3 使用:-

binascii.b2a_base64(cipher_text, newline=False)

binascii.b2a_base64(cipher_text, newline=False)

for python2 use:

对于python2使用:

binascii.b2a_base64(cipher_text)[:-1]

binascii.b2a_base64(cipher_text) [:-1]