pprint (Pretty Printer) in Python

Python | pprint (Pretty Printer): In this tutorial, we are going to learn about a very useful inbuilt module in python that is pprint with its usages and example.
Submitted by Abhinav Gangrade, on June 06, 2020

pprint

pprint is a python module that helps us to make the readability of the complex data structures easy. The pprint is also called as "pretty print".

Let us consider an example,

dictionary={'coord': {'lon': 77.22, 'lat': 28.67}, 
'weather': [{'id': 721, 'main': 'Haze', 'description': 
'haze', 'icon': '50d'}], 'base': 'stations', 'main': 
{'temp': 44, 'feels_like': 40.42, 'temp_min': 44, 
'temp_max': 44, 'pressure': 1002, 'humidity': 11}, 
'visibility': 6000, 'wind': {'speed': 4.1, 'deg': 290, 
'gust': 9.3}, 'clouds': {'all': 30}, 'dt': 1590398990, 
'sys': {'type': 1, 'id': 9165, 'country': 'IN', 
'sunrise': 1590364538, 'sunset': 1590414050}, 
'timezone': 19800, 'id': 1273294, 'name': 'Delhi', 
'cod': 200}

# This is the dictionary that we wanted to print
print(dictionary)

Now request module is of the article its just an example to create a nested data structure.

Output:

{'coord': {'lon': 77.22, 'lat': 28.67}, 
'weather': [{'id': 721, 'main': 'Haze', 'description': 'haze', 'icon': '50d'}], 
'base': 'stations', 'main': {'temp': 44, 'feels_like': 40.42, 'temp_min': 44, 
'temp_max': 44, 'pressure': 1002, 'humidity': 11}, 'visibility': 6000, 
'wind': {'speed': 4.1, 'deg': 290, 'gust': 9.3}, 'clouds': {'all': 30}, 
'dt': 1590398990, 'sys': {'type': 1, 'id': 9165, 'country': 'IN', 'sunrise': 1590364538, 'sunset': 1590414050}, 
'timezone': 19800, 'id': 1273294, 'name': 'Delhi', 'cod': 200}

As you can see, the output is not in a proper and readable way, we cannot read this complex nested dictionary structure.

To solve this issue of readability we will use the inbuilt module pprint.

Downloading pprint Module

General way: In your terminal or command prompt, type the following command,

pip install pprint

Using pycharm: Go to the project interpreter and install the module.

Now after installing, import the module and there is a function in this module named pprint so import that as

from pprint import pprint

To make the structure look good just pprint() instead of print().

# import pprint from the module pprint
from pprint import pprint


dictionary={'coord': {'lon': 77.22, 'lat': 28.67}, 
'weather': [{'id': 721, 'main': 'Haze', 'description': 
'haze', 'icon': '50d'}], 'base': 'stations', 'main': 
{'temp': 44, 'feels_like': 40.42, 'temp_min': 44, 
'temp_max': 44, 'pressure': 1002, 'humidity': 11}, 
'visibility': 6000, 'wind': {'speed': 4.1, 'deg': 290, 
'gust': 9.3}, 'clouds': {'all': 30}, 'dt': 1590398990, 
'sys': {'type': 1, 'id': 9165, 'country': 'IN', 
'sunrise': 1590364538, 'sunset': 1590414050}, 
'timezone': 19800, 'id': 1273294, 'name': 'Delhi', 
'cod': 200}

# This is the dictionary that we wanted to print
pprint(dictionary)

Output:

{'base': 'stations',
 'clouds': {'all': 30},
 'cod': 200,
 'coord': {'lat': 28.67, 'lon': 77.22},
 'dt': 1590398990,
 'id': 1273294,
 'main': {'feels_like': 40.42,
          'humidity': 11,
          'pressure': 1002,
          'temp': 44,
          'temp_max': 44,
          'temp_min': 44},
 'name': 'Delhi',
 'sys': {'country': 'IN',
         'id': 9165,
         'sunrise': 1590364538,
         'sunset': 1590414050,
         'type': 1},
 'timezone': 19800,
 'visibility': 6000,
 'weather': [{'description': 'haze', 'icon': '50d', 'id': 721, 'main': 'Haze'}],
 'wind': {'deg': 290, 'gust': 9.3, 'speed': 4.1}}
 

The above output is clear and it is easily readable.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.