python3.0 base64 error
MRAB
google at mrabarnett.plus.com
Sat Jan 17 14:10:05 EST 2009
yang michael wrote:
> I use base64 module on python3.0
> like:
>
> import base64
> b="hello world"
Try:
b = b"hello world"
instead.
> a=base64.b64encode(b)
> print(a)
>
> but when i run it,it catch a error:
> Traceback (most recent call last):
> File "/home/jackie-yang/yd5m19/pythonstudy/test.py", line 4, in <module>
> a=base64.b64encode(b)
> File "/usr/local/lib/python3.0/base64.py", line 56, in b64encode
> raise TypeError("expected bytes, not %s" % s.__class__.__name__)
> TypeError: expected bytes, not str
>
>
>
> and also the document's example:
>
>>>> import base64
>>>> encoded = base64.b64encode('data to be encoded')
>
>>>> encoded
> 'ZGF0YSB0byBiZSBlbmNvZGVk'
>>>> data = base64.b64decode(encoded)
>
>>>> data
> 'data to be encoded'
>
> can not run too.
>
> what happen?
>
As the traceback says, it needs bytes, not string. Perhaps the example
would've been clearer as:
>>> import base64
>>> encoded = base64.b64encode(b'data to be encoded')
More information about the Python-list
mailing list