printing list containing unicode string

Carsten Haese carsten at uniqsys.com
Mon Sep 10 11:12:26 EDT 2007


On Mon, 2007-09-10 at 06:59 -0700, Xah Lee wrote:
> If i have a nested list, where the atoms are unicode strings, e.g.
> 
> # -*- coding: utf-8 -*-
> ttt=[[u"→",u"↑"], [u"αβγ"],...]
> print ttt
> 
> how can i print it without getting the u'\u1234' notation?
> i.e. i want it print just like this: [[u"→"], ...]
> 
> I can of course write a loop then for each string use
> "encode("utf-8")", but is there a easier way?

It's not quite clear why you want to do this, but this is how you could
do it:

print repr(ttt).decode("unicode_escape").encode("utf-8")

However, I am getting the impression that this is a "How can I use 'X'
to achieve 'Y'?" question instead of the preferable "How can I achieve
'Y'?" type of question. In other words, printing the repr() of a list
might not be the best solution to reach the actual goal, which you have
not stated.

HTH,

-- 
Carsten Haese
http://informixdb.sourceforge.net





More information about the Python-list mailing list