This article is about a pretty useful built-in module in Python, pprint.
The pprint module provides a capability to “pretty-print” arbitrary Python data structures in a well-formatted and more readable way!
Let us consider an example:
# A python code without pprint import requests def geocode(address): resp = requests.get(url, params = {'address': address}) return resp.json() # calling geocode function data = geocode('India gate') # printing json response print(data) |
The above code is for getting the geocode information of a place using Google Maps API in JSON format.
The output of above program looks like this:
{'status': '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_address': 'Rajpath, India Gate, New Delhi, Delhi 110001,
India', 'place_id': 'ChIJC03rqdriDDkRXT6SJRGXFwc'}]}
As you can see, this output is not properly indented which affects readablity for nested data structures.
Now, consider the code below:
# A python code with pprint import requests from pprint import pprint def geocode(address): resp = requests.get(url, params = {'address': address}) return resp.json() # calling geocode function data = geocode('India gate') # pretty-printing json response pprint(data) |
The output of above code 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 did was to import the pprint function of pprint module. And use pprint() function rather than the print function!
This blog is contributed by Nikhil Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Difference between Data Scientist, Data Engineer, Data Analyst
- Difference between Data Warehousing and Data Mining
- Primitive data type vs. Object data type in Java with Examples
- MongoDB Python | Insert and Update Data
- Python | Write multiple files data to master file
- Python | Plotting Data on Google Map using pygmaps package
- Multidimensional data analysis in Python
- Python | Plotting column charts in excel sheet with data tables using XlsxWriter module
- Python | Plotting charts in excel sheet with data tools using XlsxWriter module | Set – 2
- Working With JSON Data in Python
- Retrieving And Updating Data Contained in Shelve in Python
- Python | How to copy data from one excel sheet to another
- How to delete data from file in Python
- Python | Titanic Data EDA using Seaborn
- Converting WhatsApp chat data into a Word Cloud using Python
- Exporting PDF Data using Python
- 3D Plotting sample Data from MongoDB Atlas Using Python
- Plot multiple separate graphs for same data from one Python script
- Big Data as a Technology
- Student Data Management in C++

