how to pretty-print Python dict with unicode?

Discussion in 'Python' started by kj, Aug 4, 2010.

  1. kj

    kj Guest

    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
     
    kj, Aug 4, 2010
    #1
    1. Advertising

  2. On Wed, Aug 4, 2010 at 3:15 PM, kj <> 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.
     
    Benjamin Kaplan, Aug 5, 2010
    #2
    1. Advertising

  3. 2010/8/5 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
    > --
    > 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
     
    Vlastimil Brom, Aug 5, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Hunter
    Replies:
    5
    Views:
    667
    Paul McNett
    Sep 28, 2004
  2. Sam
    Replies:
    2
    Views:
    951
    Diez B. Roggisch
    Oct 17, 2009
  3. Ethan Furman

    Python 3: dict & dict.keys()

    Ethan Furman, Jul 24, 2013, in forum: Python
    Replies:
    4
    Views:
    228
    Steven D'Aprano
    Jul 25, 2013
  4. Peter Otten

    Re: Python 3: dict & dict.keys()

    Peter Otten, Jul 24, 2013, in forum: Python
    Replies:
    1
    Views:
    97
    Neil Cerutti
    Jul 24, 2013
  5. Oscar Benjamin

    Re: Python 3: dict & dict.keys()

    Oscar Benjamin, Jul 24, 2013, in forum: Python
    Replies:
    0
    Views:
    111
    Oscar Benjamin
    Jul 24, 2013
Loading...

Share This Page