How to Format dict string outputs nicely

Viewed 61220

I wonder if there is an easy way to format Strings of dict-outputs such as this:

{
  'planet' : {
    'name' : 'Earth',
    'has' : {
      'plants' : 'yes',
      'animals' : 'yes',
      'cryptonite' : 'no'
    }
  }
}

..., where a simple str(dict) just would give you a quite unreadable ...

{'planet' : {'has': {'plants': 'yes', 'animals': 'yes', 'cryptonite': 'no'}, 'name': 'Earth'}}

For as much as I know about Python I would have to write a lot of code with many special cases and string.replace() calls, where this problem itself does not look so much like a 1000-lines-problem.

Please suggest the easiest way to format any dict according to this shape.

3 Answers
Related