Lets say I have a (nested) dictionary like this (notice the lists-values):
dic = {'level1':
{'level2':(1, 2),
'level3':
[
{'level4': (1, 2)},
{'level5': (1, 2)}
]
}
}
I am looking for a proper way to print this dictionary, I am using json
to do this:
import json
print json.dumps(dic, indent=4)
and the above code gives me the following output:
{
"level1": {
"level2": [
1,
2
],
"level3": [
{
"level4": [
1,
2
]
},
{
"level5": [
1,
2
]
}
]
}
}
While the above output is pretty good, it is still hard to read, specially if there are many levels and longer names.
I have also tried yaml
import yaml
print yaml.dump(dic)
gives the following which looks strange:
level1:
level2: !!python/tuple [1, 2]
level3:
- level4: !!python/tuple [1, 2]
- level5: !!python/tuple [1, 2]
Are there any other libraries that can produce better dumps, I think the below output is easier to read:
"level1"
|---"level2": 1, 2
|---"level3":
|---"level4": 1, 2
|---"level5": 1, 2
I believe the above is much easier to read and there may be python libraries out there that can do this.