UnicodeEncodeError -- 'character maps to <undefined>'

Discussion in 'Python' started by J, Aug 27, 2011.

  1. J

    J Guest

    Hi there,

    I'm attempting to print a dictionary entry of some twitter data to screen but every now and then I get the following error:

    (<type 'exceptions.UnicodeEncodeError'>, UnicodeEncodeError('charmap', u'RT
    @ciaraluvsjb26: BIEBER FEVER \u2665', 32, 33, 'character maps to <undefined>'), <traceback object at 0x01B323C8>)

    I have googled this but haven't really found any way to overcome the error. Any ideas?

    J
     
    J, Aug 27, 2011
    #1
    1. Advertisements

  2. Showing the actual traceback will help far more than a raw exception tuple.

    I can only try to guess what you are doing, since you haven't shown either
    any code or a traceback, but I can imagine that you're probably trying to
    encode a Unicode string into bytes, but using the wrong encoding.

    I can almost replicate the error: the exception is the same, the message is
    not, although it is similar.
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    UnicodeEncodeError: 'ascii' codec can't encode character u'\u2665' in
    position 13: ordinal not in range(128)



    The right way is to specify an encoding that includes all the characters you
    need. Unless you have some reason to choose another encoding, the best
    thing to do is to just use UTF-8.
    'BIEBER FEVER \xe2\x99\xa5'
     
    Steven D'Aprano, Aug 27, 2011
    #2
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.