2010/8/5 kj <no.em...@please.post>: > > 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 > -- > http://mail.python.org/mailman/listinfo/python-list >
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. >>> d = {1: u'\u6c17\u304c\u9055\u3046', 2: u'\u6c17\u304c\u9055\u3046', 3: >>> u'\u6c17\u304c\u9055\u3046', } >>> for k, v in d.items(): print "%s: %s" % (k, v) 1: 気が違う 2: 気が違う 3: 気が違う >>> for k, v in d.items(): print "%s: %s," % (k, v), 1: 気が違う, 2: 気が違う, 3: 気が違う, >>> >>> print "".join("%s: %s, " % (k, v) for k, v in d.iteritems()) 1: 気が違う, 2: 気が違う, 3: 気が違う, >>> 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. >>> d = {1: '\u6c17\u304c\u9055\u3046', 2: '\u6c17\u304c\u9055\u3046', 3: >>> '\u6c17\u304c\u9055\u3046', } >>> d {1: '気が違う', 2: '気が違う', 3: '気が違う'} >>> (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 -- http://mail.python.org/mailman/listinfo/python-list