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 Python 2.7 (another cool feature I really like). If I try to pretty-print an ordered dictionary, it doesn't show nicely. Instead of having each key-value pair on its own line, the whole thing shows up on one long line, which wraps many times and is hard to read.
Does anyone here have a way to make it print nicely, like the old unordered dictionaries? I could probably figure something out, possibly using the PrettyPrinter.format method, if I spend enough time, but I am wondering if anyone here already knows of a solution.
UPDATE: I filed a bug report for this. You can see it at http://bugs.python.org/issue10592.
Here is my approach to pretty print an OrderedDict
If you want to pretty print dictionary with keys in sorted order
Here's another answer that works by overriding and using the stock
pprint()
function internally. Unlike my earlier one it will handleOrderedDict
's inside another container such as alist
and should also be able to handle any optional keyword arguments given — however it does not have the same degree of control over the output that the other one afforded.It operates by redirecting the stock function's output into a temporary buffer and then word wraps that before sending it on to the output stream. While the final output produced isn't exceptionalily pretty, it's decent and may be "good enough" to use as a workaround.
Update 2.0
Simplified by using standard library
textwrap
module, and modified to work in both Python 2 & 3.Sample output:
»
{'john': 1, 'mary': 3, 'paul': 2}
»
OrderedDict([('john', 1), ('paul', 2),
('mary', 3)])
»
[OrderedDict([('john', 1), ('paul', 2),
('mary', 3)]), OrderedDict([('moe', 1),
('curly', 2), ('larry', 3)]),
OrderedDict([('weapons', 1), ('mass',
2), ('destruction', 3)])]
To print an ordered dict, e.g.
I do
Which yields
or
which yields
If the dictionary items are all of one type, you could use the amazing data-handling library
pandas
:or
There you go ^^
or
This is pretty crude, but I just needed a way to visualize a data structure made up of any arbitrary Mappings and Iterables and this is what I came up with before giving up. It's recursive, so it will fall through nested structures and lists just fine. I used the Mapping and Iterable abstract base classes from collections to handle just about anything.
I was aiming for almost yaml like output with concise python code, but didn't quite make it.
and some test data using OrderedDict and lists of OrderedDicts... (sheesh Python needs OrderedDict literals sooo badly...)
yields the following output:
I had some thoughts along the way of using str.format() for better alignment, but didn't feel like digging into it. You'd need to dynamically specify the field widths depending on the type of alignment you want, which would get either tricky or cumbersome.
Anyway, this shows me my data in readable hierarchical fashion, so that works for me!