Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #12265 > unrolled thread

UnicodeEncodeError -- 'character maps to <undefined>'

Started byJ <1jason.whatford@gmail.com>
First post2011-08-27 06:55 -0700
Last post2011-08-28 07:51 +1000
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  UnicodeEncodeError --  'character maps to <undefined>' J <1jason.whatford@gmail.com> - 2011-08-27 06:55 -0700
    Re: UnicodeEncodeError --  'character maps to <undefined>' Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-28 01:00 +1000
      Re: UnicodeEncodeError --  'character maps to <undefined>' Ben Finney <ben+python@benfinney.id.au> - 2011-08-28 07:51 +1000

#12265 — UnicodeEncodeError -- 'character maps to <undefined>'

FromJ <1jason.whatford@gmail.com>
Date2011-08-27 06:55 -0700
SubjectUnicodeEncodeError -- 'character maps to <undefined>'
Message-ID<32ed9303-96d6-4576-bb49-8b5fbefb0e30@glegroupsg2000goo.googlegroups.com>
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

[toc] | [next] | [standalone]


#12266

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-08-28 01:00 +1000
Message-ID<4e590684$0$29972$c3e8da3$5496439d@news.astraweb.com>
In reply to#12265
J wrote:

> 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>)

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


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

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.

>>> s = u'BIEBER FEVER \u2665'
>>> print s  # Printing Unicode is fine.
BIEBER FEVER ♥
>>> s.encode()  # but encoding defaults to ASCII
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.

>>> s.encode('utf-8')
'BIEBER FEVER \xe2\x99\xa5'



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#12306

FromBen Finney <ben+python@benfinney.id.au>
Date2011-08-28 07:51 +1000
Message-ID<87zkiuttf5.fsf@benfinney.id.au>
In reply to#12266
Steven D'Aprano <steve+comp.lang.python@pearwood.info> writes:

> >>> s = u'BIEBER FEVER \u2665'
> >>> print s  # Printing Unicode is fine.
> BIEBER FEVER ♥

You're a cruel man. Why do you hate me?

-- 
 \         “If nature has made any one thing less susceptible than all |
  `\    others of exclusive property, it is the action of the thinking |
_o__)              power called an idea” —Thomas Jefferson, 1813-08-13 |
Ben Finney

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web