1>>> import json
2>>> print json.dumps({'a':2, 'b':{'x':3, 'y':{'t1': 4, 't2':5}}},
3... sort_keys=True, indent=4)
4{
5 "a": 2,
6 "b": {
7 "x": 3,
8 "y": {
9 "t1": 4,
10 "t2": 5
11 }
12 }
13}
14
1# pprint (aka prettyprint) is a function that allow to print objects in a clearer way
2# Fist you need to import the pprint module
3import pprint
4# then you can create a printer whith whatever arguments you need
5my_printer = pprint.PrettyPrinter(width=20)
6# default arguments are : indent=1, width=80, depth=None, stream=None, *, compact=False, sort_dicts=True
7
8# let's take this list as an exemple:
9list1 = [[0,1,2,3,4],[1,2,3,4,5],[2,3,4,5,6],[3,4,5,6,7]]
10
11print(list1) # will print [[0,1,2,3,4],[1,2,3,4,5],[2,3,4,5,6],[3,4,5,6,7]]
12my_printer.pprint(list1) # will print:
13# [[0, 1, 2, 3, 4],
14# [1, 2, 3, 4, 5],
15# [2, 3, 4, 5, 6],
16# [3, 4, 5, 6, 7]]
17
18# You can also formant text whith pprint
19output = my_printer.pformat(list1)
20# output will be:
21"""[[0, 1, 2, 3, 4],
22 [1, 2, 3, 4, 5],
23 [2, 3, 4, 5, 6],
24 [3, 4, 5, 6, 7]]"""
1import pprint
2
3student_dict = {'Name': 'Tusar', 'Class': 'XII',
4 'Address': {'FLAT ':1308, 'BLOCK ':'A', 'LANE ':2, 'CITY ': 'HYD'}}
5
6print student_dict
7print "\n"
8print "***With Pretty Print***"
9print "-----------------------"
10pprint.pprint(student_dict,width=-1)
11