Questions tagged [pprint]

Pprint is a Python module used for “pretty-printing” arbitrary data structures.

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.

136 questions
261
votes
5 answers

How do I get python's pprint to return a string instead of printing?

In other words, what's the sprintf equivalent to pprint?
drue
  • 4,863
  • 6
  • 22
  • 14
153
votes
3 answers

Use logging print the output of pprint

I want to use pprint's output to show a complex data structure, but I would like to output it using the logging module rather than stdout. ds = [{'hello': 'there'}] logging.debug( pprint.pprint(ds) ) # outputs as STDOUT
yee379
  • 6,498
  • 10
  • 56
  • 101
118
votes
15 answers

Any way to properly pretty-print OrderedDict?

I like the pprint module in Python. I use it a lot for testing and debugging. I frequently use the width option to make sure the output fits nicely within my terminal window. It has worked fine until they added the new ordered dictionary type in…
Elias Zamaria
  • 96,623
  • 33
  • 114
  • 148
97
votes
5 answers

pprint dictionary on multiple lines

I'm trying to get a pretty print of a dictionary, but I'm having no luck: >>> import pprint >>> a = {'first': 123, 'second': 456, 'third': {1:1, 2:2}} >>> pprint.pprint(a) {'first': 123, 'second': 456, 'third': {1: 1, 2: 2}} I wanted the output to…
mulllhausen
  • 4,225
  • 7
  • 49
  • 71
40
votes
6 answers

Disabling sorting mechanism in pprint output

I have big dictionary which I`m printing for viewing with prettyprint, but how I can keep formatting but kill sorting mechanism in pprint?
Edd
  • 665
  • 1
  • 5
  • 13
32
votes
3 answers

Pretty print namedtuple

I tried pprint from pprint, but its output is just one line, there is no multiline output and no indentation.
bobzhang
  • 1,771
  • 3
  • 17
  • 19
24
votes
3 answers

pprint(): how to use double quotes to display strings?

If I print a dictionary using pprint, it always wraps strings around single quotes ('): >>> from pprint import pprint >>> pprint({'AAA': 1, 'BBB': 2, 'CCC': 3}) {'AAA': 1, 'BBB': 2, 'CCC': 3} Is there any way to tell pprint to use double quotes (")…
E.Z.
  • 6,393
  • 11
  • 42
  • 69
23
votes
5 answers

Parsing json and searching through it

I have this code import json from pprint import pprint json_data=open('bookmarks.json') jdata = json.load(json_data) pprint (jdata) json_data.close() How can I search through it for u'uri': u'http:?
BKovac
  • 455
  • 1
  • 4
  • 16
21
votes
1 answer

Python PrettyPrint output to variable

How to store a Python PrettyPrint output to some variable. Any other way than eyeD3? like this - string_output = pp.pprint(dict)
Abhishek Kulkarni
  • 3,693
  • 8
  • 35
  • 42
20
votes
6 answers

How to use pprint to print an object using the built-in __str__(self) method?

I have a Python script which processes a .txt file which contains report usage information. I'd like to find a way to cleanly print the attributes of an object using pprint's pprint(vars(object)) function. The script reads the file and creates…
hernamesbarbara
  • 6,850
  • 3
  • 26
  • 25
20
votes
2 answers

Can't get pprint to work in clojure

Noob question, using Win7 64-bit, Clojure 1.2.0, Java 1.6.0_22 When I start clojure from command line, pprint function is easily available. user=> pprint # user=> (pprint "hi") "hi" nil user=> But when I…
Sonicsmooth
  • 2,673
  • 2
  • 22
  • 35
19
votes
4 answers

Can I make pprint in python3 not split strings, like in python2?

Is there a way to tell pprint in Python 3 not to split strings on whitespace? Python 2's pprint did not do this. Can this behavior be disabled? I looked through the source for pprint and it doesn't look like there's an option I saw for this. Can I…
Carbon
  • 3,828
  • 3
  • 24
  • 51
17
votes
2 answers

How can I format a map over several lines with pprint?

pprint's docs are kind of a brick wall. If you pprint a map, it comes out on one line like so: {:a "b", :b "c", :d "e"}. Instead, I'd like to be pprinted like this, optionally with commas: {:a "b" :b "c" :d "e"} How would one do that with pprint?
Rayne
  • 31,473
  • 17
  • 86
  • 101
15
votes
5 answers

Can I avoid a sorted dictionary output after I've used pprint.pprint, in Python?

The code is: from pprint import pprint d = {"b" : "Maria", "c" : "Helen", "a" : "George"} pprint(d, width = 1) The output is: {'a': 'George', 'b': 'Maria', 'c': 'Helen'} But, the desired output is: {'b': 'Maria', 'c': 'Helen', 'a':…
Maria Pantsiou
  • 173
  • 1
  • 6
14
votes
1 answer

Pretty print JSON python

if anybody with some knowledge about pretty printing JSON could help me with this I would be extremely grateful! I'm looking to convert a complex python string into JSON format, using the function below to move the JSON string to a file: with…
Tom O' Mara
  • 267
  • 1
  • 2
  • 15
1
2 3
9 10