What is the pretty-print module in Python?
Overview
The 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 PrettyPrinter().
Syntax
pprint.pprint(object, stream = None, indent = 1, width = 80, depth = None,compact = False, sort_dicts = True, underscore_numbers = False)
Parameters
The 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 itswrite()method. Its default value isNone. 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 is1.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 is80.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 isFalse(the default) then each item of a sequence will be formatted on a separate line. If compact isTrue, the maximum items that can fit within the width will be formatted on each output line.sort_dicts: If set toTrue,dictionaries will be formatted with their keys sorted. Otherwise, they will display in insertion order. The default value isTrue.underscore_numbers: Integers will be formatted with the_character for a thousand separators. By default, it will not be displayed.
Code example
The below code demonstrates how to use the pprint() method:
import jsonfrom urllib import requestimport pprintresponse = 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)
Code explanation
In the above code:
- Lines 5 to 9: We load a dummy JSON object containing the user details from a URL. Then we print the loaded data using the normal
printmethod.
Note: When using the
- Line 12 and 15: We use the
pprint()method to pretty-print the JSON data with depth as1and width as30.
Create custom pretty-print objects
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 jsonfrom urllib import requestfrom pprint import PrettyPrinterresponse = 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)
Code explanation
In the above code:
- Lines 9 to 15: We create a new
PrettyPrinterobject with custom configuration to pretty-print the data. - Line 17: We use the
pprintmethod of thecustom_printerobject to pretty-print the user's data.
Free Resources
Attributions:
- undefined by undefined