python - Remove the new line "\n" from base64 encoded strings in Python3? -
i'm trying make https connection in python3 , when try encode username , password base64
encodebytes
method returns encoded value new line character @ end "\n" , because of i'm getting error when try connect.
is there way tell base64
library not append new line character when encoding or best way remove new line character? tried using replace
method 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
my code:
auth = b'username@domain.com:password' auth_base64 = base64.encodebytes(auth) auth_base64 = auth_base64.replace('\n', '')
any ideas? thanks
instead of encodestring
consider using b64encode
. later 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=='
it produces identical encoded string except \n
Comments
Post a Comment