how to pretty-print Python dict with unicode?

K

kj

Is there a simple way to get Python to pretty-print a dict whose
values contain Unicode? (Of course, the goal here is that these
printed values are human-readable.)

If I run the following simple script:

from pprint import pprint
x = u'\u6c17\u304c\u9055\u3046'
print '{%s: %s}' % (u'x', x)
print {u'x': x}
pprint({u'x': x})

The first print statement produces perfectly readable Japanese,
but the remaining statements both produce the line

{u'x': u'\u6c17\u304c\u9055\u3046'}

I've tried everything I can think of (including a lot of crazy
stuff) short of re-writing pprint from scratch (which I think would
be faster than grokking it and hacking at it).

Isn't there an easier way to do this?

Thanks!

~K
 
B

Benjamin Kaplan

Is there a simple way to get Python to pretty-print a dict whose
values contain Unicode?  (Of course, the goal here is that these
printed values are human-readable.)

If I run the following simple script:

from pprint import pprint
x = u'\u6c17\u304c\u9055\u3046'
print '{%s: %s}' % (u'x', x)
print {u'x': x}
pprint({u'x': x})

The first print statement produces perfectly readable Japaneuse,
but the remaining statements both produce the line

{u'x': u'\u6c17\u304c\u9055\u3046'}

I've tried everything I can think of (including a lot of crazy
stuff) short of re-writing pprint from scratch (which I think would
be faster than grokking it and hacking at it).

Isn't there an easier way to do this?

Thanks!

~K

use Python 3? http://www.python.org/dev/peps/pep-3138/

Or just iterate over the items and print them out yourself. The reason
you see the escaped values is that str(dict()) calls repr on all the
items. If you convert them to strings using str instead of repr(), it
will work the way you want.
 
V

Vlastimil Brom

2010/8/5 kj said:
Is there a simple way to get Python to pretty-print a dict whose
values contain Unicode? (Of course, the goal here is that these
printed values are human-readable.)

If I run the following simple script:

from pprint import pprint
x = u'\u6c17\u304c\u9055\u3046'
print '{%s: %s}' % (u'x', x)
print {u'x': x}
pprint({u'x': x})

The first print statement produces perfectly readable Japanese,
but the remaining statements both produce the line

{u'x': u'\u6c17\u304c\u9055\u3046'}

I've tried everything I can think of (including a lot of crazy
stuff) short of re-writing pprint from scratch (which I think would
be faster than grokking it and hacking at it).

Isn't there an easier way to do this?

Thanks!

~K

I am not sure it helps, others will probably offer more elegant suggestions,
but if the dict is one-dimensional (i.e. it doesn't contain any other
containers or mappings - dicts, lists etc., but only strings, numbers,
you can use a simple print with a string conversion in a loop or join
the dict to a single printable string.

Python 2.7 (r27:82525, Jul 4 2010, 09:01:59) [MSC v.1500 32 bit
(Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
1: $B5$$,0c$&(B
2: $B5$$,0c$&(B
3: $B5$$,0c$&(B
1: $B5$$,0c$&(B, 2: $B5$$,0c$&(B, 3: $B5$$,0c$&(B,
Or you can use python 3, where repr() behaves directly like you would
need (also for arbitrarily nested data structers, unlike the simple
approach above):

Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit
(Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
(It might be possible to replace the sys.stdout to use str() as needed
on python 2.x too, but I am not sure if it would be worth it.)

vbr
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top