取出新行" \\ n"从连接的base64在Python3 codeD字符串? [英] Remove the new line "\n" from base64 encoded strings in Python3?

查看:311
本文介绍了取出新行" \\ n"从连接的base64在Python3 codeD字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Python3一个HTTPS连接,当我尝试连接code我的用户名和密码的base64 恩codebytes 方法返回底\\ n新行字符的EN codeD价值也正因为如此,当我尝试连接,我得到一个错误。

I'm trying to make a HTTPS connection in Python3 and when I try to encode my username and password the base64 encodebytes method 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.

有没有办法告诉的base64 库不附加新行字符进行编码时或什么是删除此新行字符的最佳方式?我尝试使用替换方法,但我得到以下错误:

Is there a way to tell the base64 library 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 replace method but I get the following error:

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

我的code:

auth = b'username@domain.com:passWORD'
auth_base64 = base64.encodebytes(auth)
auth_base64 = auth_base64.replace('\n', '')

任何想法?谢谢

推荐答案

考虑使用 b64en code 。后来不添加 \\ n 字符。例如。

Instead of encodestring consider using b64encode. Later does not add \n characters. e.g.

In [11]: auth = b'username@domain.com:passWORD'

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

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

它产生相同的连接,除了codeD字符串 \\ n

这篇关于取出新行&QUOT; \\ n&QUOT;从连接的base64在Python3 codeD字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆