Prev: scipy / stats : quantiles using sample weights from survey data
Next: Python 2.6.6 release candidate 1 now available
From: kj on 4 Aug 2010 18:15 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
From: Benjamin Kaplan on 4 Aug 2010 19:24 On Wed, Aug 4, 2010 at 3:15 PM, kj <no.email(a)please.post> wrote: > > > > 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.
From: Vlastimil Brom on 4 Aug 2010 19:28
2010/8/5 kj <no.email(a)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: $B5$$,0c$&(B 2: $B5$$,0c$&(B 3: $B5$$,0c$&(B >>> for k, v in d.items(): print "%s: %s," % (k, v), 1: $B5$$,0c$&(B, 2: $B5$$,0c$&(B, 3: $B5$$,0c$&(B, >>> >>> print "".join("%s: %s, " % (k, v) for k, v in d.iteritems()) 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. >>> d = {1: '\u6c17\u304c\u9055\u3046', 2: '\u6c17\u304c\u9055\u3046', 3: '\u6c17\u304c\u9055\u3046', } >>> d {1: '$B5$$,0c$&(B', 2: '$B5$$,0c$&(B', 3: '$B5$$,0c$&(B'} >>> (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 |