Re: python3.0 base64 error

Discussion in 'Python' started by MRAB, Jan 17, 2009.

  1. MRAB

    MRAB Guest

    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')
    MRAB, Jan 17, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Si4gTmFkYWw=?=

    Base64 Error/Hotfix?

    =?Utf-8?B?Si4gTmFkYWw=?=, Nov 7, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,712
    =?Utf-8?B?Si4gTmFkYWw=?=
    Nov 7, 2005
  2. Christoph Krammer

    Error on base64.b64decode() ?!

    Christoph Krammer, Oct 12, 2007, in forum: Python
    Replies:
    2
    Views:
    4,966
    Tim Roberts
    Oct 14, 2007
  3. Jean-Paul Calderone

    Re: Error on base64.b64decode() ?!

    Jean-Paul Calderone, Oct 12, 2007, in forum: Python
    Replies:
    0
    Views:
    445
    Jean-Paul Calderone
    Oct 12, 2007
  4. Andrew Berg
    Replies:
    0
    Views:
    330
    Andrew Berg
    Jun 16, 2012
  5. Tobias Marquardt
    Replies:
    1
    Views:
    207
    Hans Mulder
    Oct 27, 2012
Loading...

Share This Page