FAQ
Hello everyone,

I am trying to encode a string using b4encode and I get the following
error :
b64encode(u"Salut Pierre, comment ?a va?")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/base64.py", line 53, in b64encode
encoded = binascii.b2a_base64(s)[:-1]
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe7' in
position 22: ordinal not in range(128)

If I remove the "u" infront of the string, it works. The problem is that
I in my program, the string is given to me un unicode/utf-8. I tried
several things, but I still get it, How can I get it to work, anybody
have any idea?

Thanks,
Gabriel

Search Discussions

  • Peter Otten at May 26, 2008 at 10:34 am

    Gabriel Rossetti wrote:

    Hello everyone,

    I am trying to encode a string using b4encode and I get the following
    error :
    b64encode(u"Salut Pierre, comment ?a va?")
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/usr/lib/python2.5/base64.py", line 53, in b64encode
    encoded = binascii.b2a_base64(s)[:-1]
    UnicodeEncodeError: 'ascii' codec can't encode character u'\xe7' in
    position 22: ordinal not in range(128)

    If I remove the "u" infront of the string, it works. The problem is that
    I in my program, the string is given to me un unicode/utf-8. I tried
    several things, but I still get it, How can I get it to work, anybody
    have any idea?
    base64.b64encode(u"Salut Pierre, comment ?a va?".encode("utf8"))
    'U2FsdXQgUGllcnJlLCBjb21tZW50IMOnYSB2YT8='

    unicode is a sequence of codepoints with no predetermined representation as
    a byte sequence. Therefore whenever you go from unicode to str you have to
    decide upon an encoding. If you don't make that decision python assumes
    ascii which will of course fail for non-ascii characters.

    Peter
  • Gabriel Rossetti at May 26, 2008 at 12:32 pm

    Peter Otten wrote:
    Gabriel Rossetti wrote:

    Hello everyone,

    I am trying to encode a string using b4encode and I get the following
    error :
    b64encode(u"Salut Pierre, comment ?a va?")
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/usr/lib/python2.5/base64.py", line 53, in b64encode
    encoded = binascii.b2a_base64(s)[:-1]
    UnicodeEncodeError: 'ascii' codec can't encode character u'\xe7' in
    position 22: ordinal not in range(128)

    If I remove the "u" infront of the string, it works. The problem is that
    I in my program, the string is given to me un unicode/utf-8. I tried
    several things, but I still get it, How can I get it to work, anybody
    have any idea?
    base64.b64encode(u"Salut Pierre, comment ?a va?".encode("utf8"))
    'U2FsdXQgUGllcnJlLCBjb21tZW50IMOnYSB2YT8='

    unicode is a sequence of codepoints with no predetermined representation as
    a byte sequence. Therefore whenever you go from unicode to str you have to
    decide upon an encoding. If you don't make that decision python assumes
    ascii which will of course fail for non-ascii characters.

    Peter
    Ok, I get it better now, thanks!
    Gabriel
  • Max M at May 26, 2008 at 10:43 am

    Gabriel Rossetti skrev:
    Hello everyone,

    I am trying to encode a string using b4encode and I get the following
    error :
    b64encode(u"Salut Pierre, comment ?a va?")
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/usr/lib/python2.5/base64.py", line 53, in b64encode
    encoded = binascii.b2a_base64(s)[:-1]
    UnicodeEncodeError: 'ascii' codec can't encode character u'\xe7' in
    position 22: ordinal not in range(128)

    If I remove the "u" infront of the string, it works. The problem is that
    I in my program, the string is given to me un unicode/utf-8. I tried
    several things, but I still get it, How can I get it to work, anybody
    have any idea?
    I have written about it here:

    http://mxm-mad-science.blogspot.com/2008/03/python-unicode-lessons-from-school-of.html

    The reason/solution for your problem is mentioned somewhere below the
    middle.


    --

    hilsen/regards Max M, Denmark

    http://www.mxm.dk/
    IT's Mad Science
  • Gabriel Rossetti at May 26, 2008 at 12:31 pm

    Max M wrote:
    Gabriel Rossetti skrev:
    Hello everyone,

    I am trying to encode a string using b4encode and I get the following
    error :
    b64encode(u"Salut Pierre, comment ?a va?")
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/usr/lib/python2.5/base64.py", line 53, in b64encode
    encoded = binascii.b2a_base64(s)[:-1]
    UnicodeEncodeError: 'ascii' codec can't encode character u'\xe7' in
    position 22: ordinal not in range(128)

    If I remove the "u" infront of the string, it works. The problem is
    that I in my program, the string is given to me un unicode/utf-8. I
    tried several things, but I still get it, How can I get it to work,
    anybody have any idea?
    I have written about it here:

    http://mxm-mad-science.blogspot.com/2008/03/python-unicode-lessons-from-school-of.html


    The reason/solution for your problem is mentioned somewhere below the
    middle.
    Thank you, it's a bit clearer now, I thought unicode and utf8 were
    synonyms, but from what I understood unicode is sort of like bytecode,
    and utf8/latin1/etc would be like a asm dialect that bytecode would be
    translated to.

    Thank you,
    Gabriel

    --
    Arimaz SA
    Av. du 24 Janvier 11
    Ateliers de la Ville de Renens, Atelier 5
    1020 Renens, Switzerland
    www.arimaz.com
    www.mydeskfriend.com
    Mob: +41-(0)79-539-0069
    Tel: +41-(0)21-566-7343

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMay 26, '08 at 10:14a
activeMay 26, '08 at 12:32p
posts5
users3
websitepython.org

People

Translate

site design / logo © 2023 Grokbase