Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > A short question about non-ascii characters in list

Reply
Thread Tools

A short question about non-ascii characters in list

 
 
js
Guest
Posts: n/a
 
      09-17-2007
>>> print u"äöü"
äöü
>>> print [u"äöü"]

[u'\xe4\xf6\xfc']

Python seems to treat non-ASCII chars in a list differently from the
one in the outside of a list.
I think this behavior is so inconvenient and actually makes debugging
work harder.

Is this an intentional? Is there any doc discussing about this?

Thanks.
 
Reply With Quote
 
 
 
 
Dan Bishop
Guest
Posts: n/a
 
      09-17-2007
On Sep 17, 12:08 am, js <ebgs...@gmail.com> wrote:
> >>> print u"äöü"

> äöü
> >>> print [u"äöü"]

>
> [u'\xe4\xf6\xfc']
>
> Python seems to treat non-ASCII chars in a list differently from the
> one in the outside of a list.
> I think this behavior is so inconvenient and actually makes debugging
> work harder.
>
> Is this an intentional? Is there any doc discussing about this?


It's intentional. __str__ of a list uses the __repr__ of its
elements. This helps reduce confusion (e.g., between ['a', 'b, c']
and ['a, b', 'c']).

 
Reply With Quote
 
 
 
 
js
Guest
Posts: n/a
 
      09-17-2007
Thank you for your quick reply.

> It's intentional. __str__ of a list uses the __repr__ of its
> elements. This helps reduce confusion (e.g., between ['a', 'b, c']
> and ['a, b', 'c']).

That's make sence, but it's also true that
sometimes we want to see the contents of a list in pretty format.
So for now I need to write and use crappy mylist like this.

class mylist(list):
def __str__(self):
return '[' + ', '.join(self) + ']'

l = mylist([u"äöü", u"äöü", u"äöü"])
print unicode(l)


very ugly, but just works.
 
Reply With Quote
 
Bjoern Schliessmann
Guest
Posts: n/a
 
      09-17-2007
js wrote:
> That's make sence, but it's also true that
> sometimes we want to see the contents of a list in pretty format.


That may be true, but most of the time not (at least not me) --
lists are no pretty printing instrument, but a container.

> So for now I need to write and use crappy mylist like this.
>
> class mylist(list):
> def __str__(self):
> return '[' + ', '.join(self) + ']'
>
> l = mylist([u"äöü", u"äöü", u"äöü"])
> print unicode(l)
>
>
> very ugly, but just works.


What's wrong with

print ",".join(mylist)

? Also, the your solution isn't really "ugly" in my opinion.

Regards,


Björn

--
BOFH excuse #55:

Plumber mistook routing panel for decorative wall fixture

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference of extern short *x and extern short x[]? Andre C Programming 5 07-17-2012 07:38 PM
unsigned short, short literals Ioannis Vranos C Programming 5 03-05-2008 01:25 AM
longs, long longs, short short long ints . . . huh?! David Geering C Programming 15 01-11-2007 09:39 PM
unsigned short short? slougheed@gmail.com C++ 4 10-16-2006 11:25 PM
Win XP Pro x64 Software Compatiblility List - Informal Short List =?Utf-8?B?RGVhdGhOQUNhbg==?= Windows 64bit 4 09-03-2006 11:25 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57