How do you decode Base64 data in Python?

How do you decode Base64 data in Python?

import base64
coded_string = Q5YACgA...
base64.b64decode(coded_string)

worked for me. At the risk of pasting an offensively-long result, I got:

>>> base64.b64decode(coded_string)
2: Cx96x00nx00x00x00x00Cx96x00x1bx00x00x00x00Cx96x00-x00x00x00x00Cx96x00?x00x00x00x00Cx96x07Mx00x00x00x00Cx96x07_x00x00x00x00Cx96x07px00x00x00x00Cx96x07x82x00x00x00x00Cx96x07x94x00x00x00x00Cx96x07xa6Cqxf0x7fCx96x07xb8DJx81xc7Cx96x07xcaDxa5x9dtCx96x07xdcDxb6x97x11Cx96x07xeeDx8bx8flCx96x07xffDx03xd4xaaCx96x08x11Bx05&xdcCx96x08#x00x00x00x0[email protected]x9bCx96x08}x00x00x00x00Cx96x08x8ex00x00x00x00Cx96x08xa0x00x00x00x00Cx96x08xb2x00x00x00x00Cx96x86xf9x00x00x00x00Cx96x87x0bx00x00x00x00Cx96x87x1dx00x00x00x00Cx96x87/x00x00x00x00Cx96x87AAx0bxe7PCx96x87SCIxf5gCx96x87eCxd4JxeaCx96x87wDrx17ECx96x87x89Dx00F6Cx96x87x9bCx9cgxdeCx96x87xadBxd56x0cCx96x87xbfx00x00x00x00Cx96x87xd1x00x00x00x00Cx96x87xe3x00x00x00x00Cx96x87xf5x00x00x00x00Cx9cY}x00x00x00x00Cx9cYx90x00x00x00x00Cx9cYxa4x00x00x00x00Cx9cYxb7x00x00x00x00Cx9cYxcbCx1fxbdxa3Cx9cYxdeCCz{Cx9cYxf1CDx02xa7Cx9cZx05C+x9dx97Cx9cZx18Cx03Rxe3Cx9cZ,x00x00x00x00Cx9cZ?
[stuff omitted as it exceeded SOs body length limits]
xbbx00x00x00x00Dxc5!7x00x00x00x00Dxc5!xb2x00x00x00x00Dxc7x14xx00x00x00x00Dxc7x14xf6x00x00x00x00Dxc7x15tx00x00x00x00Dxc7x15xf2x00x00x00x00Dxc7x16pC5x9fxf9Dxc7x16xeeC[xb5xf5Dxc7x17lCGx1b;Dxc7x17xeaBxe3x0bxa6Dxc7x18hx00x00x00x00Dxc7x18xe6x00x00x00x00Dxc7x19dx00x00x00x00Dxc7x19xe2x00x00x00x00Dxc7xfexb4x00x00x00x00Dxc7xff3x00x00x00x00Dxc7xffxb2x00x00x00x00Dxc8x001x00x00x00x00

What problem are you having, specifically?

Python 3 (and 2)

import base64
a = eW91ciB0ZXh0
base64.b64decode(a)

Python 2

A quick way to decode it without importing anything:

eW91ciB0ZXh0.decode(base64)

or more descriptive

>>> a = eW91ciB0ZXh0
>>> a.decode(base64)
your text

How do you decode Base64 data in Python?

Base64 encode/decode example:

import base64

mystr = O João mordeu o cão!

# Encode
mystr_encoded = base64.b64encode(mystr.encode(utf-8))
# bTyBKb8OjbyBtb3JkZXUgbyBjw6NvIQ==

# Decode
mystr_encoded = base64.b64decode(mystr_encoded).decode(utf-8)
# O João mordeu o cão!

Leave a Reply

Your email address will not be published.