Suppress unicode prefix on strings when using pprint
Asked Answered
P

4

6

Is there any clean way to supress the unicode character prefix when printing an object using the pprint module?

>>> import pprint
>>> pprint.pprint({u'foo': u'bar', u'baz': [u'apple', u'orange', u'pear', u'guava', u'banana'], u'hello': u'world'})
{u'baz': [u'apple', u'orange', u'pear', u'guava', u'banana'],
 u'foo': u'bar',
 u'hello': u'world'}

This looks pretty ugly. Is there any way to print the __str__ value of each object, instead of the __repr__?

Politico answered 2/6, 2013 at 23:51 Comment(4)
If you print the __str__ of each object, you won't get the quotes either. Is that really what you want?Solander
yes, that's what i wantPolitico
Then why didn't you say so, and why did you accept an answer that doesn't remove the quotes?Solander
The accepted answer is programmable, so you can adapt it to whatever format you need.Politico
C
10

It could be done by overriding the format method of the PrettyPrinter object, and casting any unicode object to string:

import pprint

def my_safe_repr(object, context, maxlevels, level):
    typ = pprint._type(object)
    if typ is unicode:
        object = str(object)
    return pprint._safe_repr(object, context, maxlevels, level)

printer = pprint.PrettyPrinter()
printer.format = my_safe_repr
printer.pprint({u'foo': u'bar', u'baz': [u'apple', u'orange', u'pear', u'guava', u'banana'], u'hello': u'world'})

which gives:

{'baz': ['apple', 'orange', 'pear', 'guava', 'banana'],
 'foo': 'bar',
 'hello': 'world'}
Cryptozoite answered 3/6, 2013 at 0:38 Comment(3)
Unfortunately it gives exception UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128) when i.e. {u'ключ': u'значение'}Saffron
Indeed, your strings are pure unicode strings, so they cannot be cast to ASCII. You should not consider removing the unicode prefix, because it makes no sense in your case.Cryptozoite
I solved this problem by encoding value before convert it into strSaffron
A
2

This may be too much, but one possible way is to implement a wrapper over the output stream:

import pprint,sys,re

class writer :
    def write(self, text):
        text=re.sub(r'u\'([^\']*)\'', r'\1',text)
        sys.stdout.write(text)

wrt=writer()
d = { u'foo': u'bar', u'baz':
        [u'apple', u'orange', u'pear', u'guava', u'banana'],
        u'hello': u'world'}
pp = pprint.PrettyPrinter(stream=wrt)

pp.pprint(d)

Output:

{baz: [apple, orange, pear, guava, banana],
 foo: bar,
 hello: world}

It is also possible to put quotes inside parents to have single quotes around strings, e,g, 'foo' : 'bar':

text=re.sub(r'u(\'[^\']*\')', r'\1',text)

This gives:

{'baz': ['apple', 'orange', 'pear', 'guava', 'banana'],
 'foo': 'bar',
 'hello': 'world'}
Anatolian answered 3/6, 2013 at 0:33 Comment(0)
T
1

No, pprint prints the representation. It's not for making pretty end-user output, but for printing Python objects in a readable way.

The pprint module provides a capability to “pretty-print” arbitrary Python data structures in a form which can be used as input to the interpreter.

Tucana answered 3/6, 2013 at 0:16 Comment(3)
Any good alternative way to print a large, nested structure in a readable way?Politico
Hmm, I found the prettyprint module which looks pretty nice.Politico
interesting answer @jterrace! cooked down this is just: use print(json.dumps())Spectacular
P
1

(Many years later answer)

Just use python3. All standard strings are unicode by default, so no u prefix.

Proleg answered 19/5, 2020 at 8:23 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.