Reputation: 1395
Im looking to print a nested dictionary in a nice clean readable format. I have done the standard approach of printing dictionary keys and their values but as this is nested it doesnt present very well. Could someone explain how I could achieve the below formatting?
Host Port Service
192.168.1.200 80 IIS 7.5
443 IIS 7.5
192.168.1.201 22 SSHv.199
Dictionary Example
192.168.1.200 {'3389': 'Microsoft Terminal Service', '49160': 'Microsoft Windows RPC', '49163': 'Microsoft Windows RPC', '135': 'Microsoft Windows RPC', '49152': 'Microsoft Windows RPC', '49153': 'Microsoft Windows RPC'}
Upvotes: 1
Views: 170
Reputation: 5115
You can use formatting options with json.dumps
:
>>> d={'server1':{'3389': 'Microsoft Terminal Service', '49160': 'Microsoft Windows RPC', '49163': 'Microsoft Windows RPC', '135': 'Microsoft Windows RPC', '49152': 'Microsoft Windows RPC', '49153':'Microsoft Windows RPC'},'server2':{'morekeys':'morevalues'}}
>>> print json.dumps(d, indent=4)
{
"server1": {
"3389": "Microsoft Terminal Service",
"49160": "Microsoft Windows RPC",
"49163": "Microsoft Windows RPC",
"135": "Microsoft Windows RPC",
"49152": "Microsoft Windows RPC",
"49153": "Microsoft Windows RPC"
},
"server2": {
"morekeys": "morevalues"
}
}
Upvotes: 3