0

I'm a python newbie and trying to to some very simple task and encode a text to base64. I understand that I have to pad my string but the problem is the that padding is also been encoded..:/ this is my code:

import base64

b64Val = b64encode(b'this is a test')
print(b64Val)

and the result is:

b'dGhpcyBpcyBhIHRlc3Q='

My question is ofcource how to remove the b' and the quots from the generated code? i tried to use this thread Base64 encoding in Python 3 but apperantly its not working the same as I do get the entire string including the padding chars... going crazy here.. thanks!!

Community
  • 1
  • 1
Moran
  • 35
  • 5

1 Answers1

0

They will go away as soon as you write it to a byte-oriented store. Since, of course, they aren't actually there in the first place.

with open('foo.txt', 'wb') as f:
  f.write(b64Val)
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Thanks Ignacio! Although im not going to use this in a text file but in a header - you did gave me an idea of how to achieve it.. i can do it by encoding the string to UTF-8 and after the base64 encoding, decode the base64 using the same UTF-8. working just fine! :) – Moran Nov 26 '13 at 17:53