304 North Cardinal St.
Dorchester Center, MA 02124
pprint module can be used to pretty-print the Python data structures in a readable format. The
pprint is a native Python library.
We can use the
pprint() method or create our
pprint object with
pprint.pprint(object, stream = None, indent = 1, width = 80, depth = None,
compact = False, sort_dicts = True, underscore_numbers = False)
pprint() method takes the following parameters:
object: It is the object to be pretty-printed.
stream: It is a file-like object to which the output is written by calling its
write()method. Its default value is
None. This can be used when we want to write the pretty-print data to a file or a stream.
indent: It denotes the amount of space added for each nesting level. The default value is
width: It denotes the maximum number of characters per line in the output. If the characters exceed the width, the remaining will be wrapped to the next line. The default value is
depth: It is the number of depth levels (nested data types) to be displayed. If the depth level of data is beyond the configured depth limit, then
...will be displayed. By default, it will show all data.
compact: If compact is
False(the default) then each item of a sequence will be formatted on a separate line. If compact is
True, the maximum items that can fit within the width will be formatted on each output line.
sort_dicts: If set to
True,dictionaries will be formatted with their keys sorted. Otherwise, they will display in insertion order. The default value is
underscore_numbers: Integers will be formatted with the
_character for a thousand separators. By default, it will not be displayed.
The below code demonstrates how to use the
import json from urllib import request import pprint response = request.urlopen("https://jsonplaceholder.typicode.com/users/2") json_response = response.read() users = json.loads(json_response) print("Without pretty print") print(users) print("With pretty print") pprint.pprint(users) print("With pretty print and depth 1 width 30") pprint.pprint(users, depth = 1, width= 30)
In the above code:
Note: When using the
pprint()method to pretty-print the JSON data with depth as
1and width as
We can configure the default configurations of the
pprint method by creating the new
PrettyPrinter() object. So we can use the object to pretty-print without specifying the configuration on each function call.
import json from urllib import request from pprint import PrettyPrinter response = request.urlopen("https://jsonplaceholder.typicode.com/users/2") json_response = response.read() users = json.loads(json_response) custom_printer = PrettyPrinter( indent=4, width=100, depth=2, compact=True, sort_dicts=False ) custom_printer.pprint(users)
In the above code:
PrettyPrinterobject with custom configuration to pretty-print the data.
pprintmethod of the
custom_printerobject to pretty-print the user’s data.
In our experience, we suggest you solve this What is the pretty-print module in Python? and gain some new skills from Professionals completely free and we assure you will be worth it.
If you are stuck anywhere between any coding problem, just visit Queslers to get the What is the pretty-print module in Python?
I hope this What is the pretty-print module in Python? would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
More Coding Solutions >>