unicode bit me

Scott David Daniels Scott.Daniels at Acm.Org
Fri May 8 11:47:11 EDT 2009


anuraguniyal at yahoo.com wrote:
> #how can I print a list of object which may return unicode
> representation?
> # -*- coding: utf-8 -*-
> 
> class A(object):
> 
>     def __unicode__(self):
>         return u"©au"
> 
>     __str__ = __repr__ = __unicode__
> 
> a = A()
> 
> try:
>     print a # doesn't work?
> except UnicodeEncodeError,e:
>     print e
> try:
>     print unicode(a) # works, ok fine, great
> except UnicodeEncodeError,e:
>     print e
> try:
>     print unicode([a]) # what!!!! doesn't work?
> except UnicodeEncodeError,e:
>     print e
> """
> Now how can I print a list of object which may return unicode
> representation?
> loop/map is not an option as it goes much deepr in my real code
> any can anyoen explain what is happening here under the hood?
> """

<rant>It would be a bit easier if people would bother to mention
their Python version, as we regularly get questions from people
running 2.3, 2.4, 2.5, 2.6, 2.7a, 3.0, and 3.1b.  They run computers
with differing operating systems and versions such as: Windows 2000,
OS/X Leopard, ubuntu Hardy Heron, SuSE, ....

You might shocked to learn that a good answer often depends on the
particular situation above.  Even though it is easy to say, for example:
     platform.platform()  returns  'Windows-XP-5.1.2600-SP3'
     sys.version is
'2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)]'
</rant>

What is happening is that print is writing to sys.stdout, and
apparently that doesn't know how to send unicode to that destination.
If you are running under IDLE, print goes to the output window, and
if you are running from the command line, it is going elsewhere.
the encoding that is being used for output is sys.stdout.encoding.

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list