pprint: data printer in Python

print_r in | Python Methods and Functions

The pprint module provides the ability to "nicely print" arbitrary Python data structures in a well-formatted and more readable way!

Let's take an example:

# Python code without pprint

import requests

 

def geocode (address):

url = " https://maps.googleapis.com/maps/api/geocode/json "

  resp = requests.get (url, params = { 'address' : address})

return resp.json ()

 
# call the geocode function

data = geocode ( 'India gate' )

  
# print json response

print (data)

The above code is for getting information about geocoding a place using the Google Maps API in JSON format.

The output of the above program looks like this:

 {'st atus': 'OK',' results': [{'address_components': [{' long_name': 'Rajpath',' types': ['route'],' short_name': 'Rajpath'}, {' long_name' : 'India Gate',' types': ['political',' sublocality', 'sublocality_level_1'],' short_name': 'India Gate'}, {' long_name': 'New Delhi',' types': [' locality', 'political'],' short_name': 'New Delhi'}, {' long_name': 'New Delhi',' types': ['administrative_area_level_2',' political'], 'short_name':' New Delhi' }, {'long_name':' Delhi', 'types': [' administrative_area_level_1', 'political'],' short_name': 'DL'}, {' long_name': 'India',' types': ['country ',' political'], 'short_name':' IN'}, {'long_name':' 110001', 'types': [' postal_code'], 'short_name':' 110001'}], 'geometry': { 'location': {' lng': 77.2295097, 'lat': 28.612912},' viewport': {'northeast': {' lng': 77.2308586802915, 'lat': 28.6142609802915},' southwest': {'lng': 77.22816071970848, 'lat': 28.6115630197085}},' location_type': 'APPROXIMATE'},' types': ['establishment',' point_of_interest'], 'formatted_addr ess': 'Rajpath, India Gate, New Delhi, Delhi 110001, India',' place_id': 'ChIJC03rqdriDDkRXT6SJRGXFwc'}]} 

As you can see, this output is not indented correctly, which affects readability for nested data structures.

Now consider the code below:

# Python code with pprint

import requests

from pprint import pprint

  

def geocode (address):

url = " https://maps.googleapis.com/maps/api/geocode/json "

resp = requests.get (url, params = { ' address' : address})

return resp.json ()

 
# call the geocode function

data = geocode ( 'India gate' )

 
# pretty-printing json response
pprint (data)

Output of the above code but looks like this:

  {'results': [{' address_components': [{'long_name':' Rajpath', 'short_name':' Rajpath', 'types': [' route']}, {'long_name':' India Gate', 'short_name':' India Gate', 'types': [' political', 'sublocality',' sublocality_level_1']}, {'long_name':' New Delhi', 'short_name':' New Delhi', 'types': [' locality', 'political']}, {' long_name': 'New Delhi',' short_name': 'New Delhi',' types': ['administrative_area_level_2',' political']}, {'long_name':' Delhi', 'short_name':' DL', 'types': [' administrative_area_level_1', 'political']}, {' long_name': 'India ',' short_name': 'IN',' types': ['country',' political']}, {'long_name':' 110001', 'short_name':' 110001', 'types': [' postal_code' ]}], 'formatted_address':' Rajpath, India Gate, New Delhi, Delhi '' 110001, India', 'geometry': {' location': {'lat': 28.612912,' lng': 77.2295097}, 'location_type ':' APPROXIMATE', 'viewport': {' northeast': {'lat': 28.6142609802915,' lng': 77.2308586802915}, 'southwest': {' lat': 28.6115630197085, 'lng': 77.22816071970848}}},' place_id': 'ChIJC03rqdriDDkRXT6SJRGXFwc',' types': ['establishment',' point_of_interest']}], 'status':' OK'}  

As you can see, the output is now well formatted and much more readable.

All we have done is to import the pprint function of the pprint module. And use the pprint () function instead of the print function!

This blog is provided by Nikhil Kumar . If you like Python.Engineering and would like to contribute, you can also write an article using contrib.python.engineering, or email your article to [email protected] See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.





pprint: data printer in Python: StackOverflow Questions