If you try out a Python package in the REPL, the output of print() may not be the most useful one. In Ruby I used for this case the class PrettyPrint and as it turns out, Python has a similar built-in feature. Let’s have a look.
This post is part of my journey to learn Python. You can find the other parts of this series here. You find the code for this post in my PythonFriday repository on GitHub.
The problem with print()
The print() method works great for a single line of text that you want to write to the console. When you try to write a list or a dictionary, print() will write them in one single line. This list of dictionaries with nested dictionaries is a good example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
data = [ { 'name': "Rebecca Stephenson", 'phone': "(154) 221-8558", 'zipCode': "900185", 'country': "South Korea", 'options': ['a','b','c'], 'total': "$74.79" }, { 'name': "Amos Nieves", 'phone': "1-762-301-2264", 'zipCode': "25566", 'country': "Russian Federation", 'options': { 'a': 'full', 'f': 'partial', 'c': {'k1': 1, 'k2': 3} }, 'total': "$21.78" } ] |
If we use print() for the output it looks like this:
1 2 3 |
>>> print(data) [{'name': 'Rebecca Stephenson', 'phone': '(154) 221-8558', 'zipCode': '900185', 'country': 'South Korea', 'options': ['a', 'b', 'c'], 'total': '$74.79'}, {'name': 'Amos Nieves', 'phone': '1-762-301-2264', 'zipCode': '25566', 'country': 'Russian Federation', 'options': {'a': 'full', 'f': 'partial', 'c': {'k1': 1, 'k2': 3}}, 'total': '$21.78'}] |
PrettyPrinter for Python
Python comes with a PrettyPrinter out of the box. All you need to do is to import pprint and instantiate the PrettyPrinter class:
1 2 |
import pprint pp = pprint.PrettyPrinter() |
The object from above looks a lot better when we print it with the PrettyPrinter:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
>>> pp.pprint(data) [{'country': 'South Korea', 'name': 'Rebecca Stephenson', 'options': ['a', 'b', 'c'], 'phone': '(154) 221-8558', 'total': '$74.79', 'zipCode': '900185'}, {'country': 'Russian Federation', 'name': 'Amos Nieves', 'options': {'a': 'full', 'c': {'k1': 1, 'k2': 3}, 'f': 'partial'}, 'phone': '1-762-301-2264', 'total': '$21.78', 'zipCode': '25566'}] |
Options for PrettyPrinter
PrettyPrinter has a few options to influence the output. If we indent the objects and reduce the width for our output, PrettyPrinter may even better show the nested values:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
>>> pp = pprint.PrettyPrinter(indent=5, depth=10, width=40, compact=False) >>> pp.pprint(data) [ { 'country': 'South Korea', 'name': 'Rebecca Stephenson', 'options': ['a', 'b', 'c'], 'phone': '(154) 221-8558', 'total': '$74.79', 'zipCode': '900185'}, { 'country': 'Russian Federation', 'name': 'Amos Nieves', 'options': { 'a': 'full', 'c': { 'k1': 1, 'k2': 3}, 'f': 'partial'}, 'phone': '1-762-301-2264', 'total': '$21.78', 'zipCode': '25566'}] |
Conclusion
This little class is a great help when you need to inspect a dictionary in the console. We don’t need to install any packages and can use it in any Python version released after 1997.
2 thoughts on “Python Friday #93: Pretty Print for Python”