Hello, readers! In this article, we will be focusing on Python pprint module, in detail. pprint() is short for Pretty Print and the main functionality of this tool is to make the output prettier to look at!
So, let us begin!!
Table of Contents
Python print() function limitations
Python offers us varieties of functions to deal with the data at the same time manipulate it according to the user requirement.
The basic function to get the user outcome to the screen is the python print() function. With this function, we can get the resultant of our manipulation on the screen.
But when it comes to API calls which involves huge amount of JSON data key-value pairs to be loaded, print() function kind of fails to give us the necessary output in a proper format.
Let us have a look at the below example–
import requests import json from pprint import pprint api = requests.get('https://api.covid19india.org/state_district_wise.json') info = api.text json_data = json.loads(info) print(json_data)
As seen below, in this example, we get the data in an unstructured format that makes it difficult to understand and interpret directly.
Python pprint module – Our Rescue!
No matter what is the programming language being used, presentation of data has always played a crucial role. It enables us to understand the data in a better and clear format.
The data that is produced as an outcome from any specific modules does not always necessarily stay with the development team. It can also circulate to other discipline within the company fort analysis and further use. that is why the presentation of data really matters.
Plus, if the data is presented in an unclear, ambiguous format, it would definitely lead to re-work. Thus, reducing the efficiency of the model.
For example, when we deal with API calls, the resultant JSON data is usually tremendous in size and form. It such scenario, usual print() function cannot do justice to the presentation of the output on the screen.
This is when Python pprint module comes into the picture. With pprint module, we can easily format the data in a pretty format. It aligns the data in a structured format giving it a pretty outline.
Let us now try to implement and observe the data presentation using pprint module in the upcoming sections.
Example :: Python pprint module during API calls
In the below example, we have made an API call to the below API regards to COVID mentioned–
For the same, we need to import the requests module along with JSON module to load the data into JSON form. Further, we have imported the pprint module to make use of pprint() function to format the data in a pretty format.
import requests import json from pprint import pprint api = requests.get('https://api.covid19india.org/state_district_wise.json') info = api.text json_data = json.loads(info) pprint(json_data)
By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question along this topic.
Try implementing the concept of pprint module with various different data and let us know your understanding in the comment box.
For more such posts related to Python programming, stay tuned with us.
Till then, Happy Learning!! 🙂
Reference – Official Documentation