views:

74

answers:

3

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.

+8  A: 

Use pprint

import pprint

x  = {
  'planet' : {
    'name' : 'Earth',
    'has' : {
      'plants' : 'yes',
      'animals' : 'yes',
      'cryptonite' : 'no'
    }
  }
}
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(x)

This outputs

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

Play around with pprint formatting and you can get the desired result.

pyfunc
Well, it has only depth, width and indent as parameters. I don't see a way to make planet go to the next line. Sure, PP results are better readable, but the newline+appropriate_spaces is nessesary for my case.But thanks for showing me this nice package. Comes in very handy in other cases.
erikb
+3  A: 
def format(d, tab=0):
    s = ['{\n']
    for k,v in d.items():
        if isinstance(v, dict):
            v = format(v, tab+1)
        else:
            v = repr(v)

        s.append('%s%r: %s,\n' % ('  '*tab, k, v))
    s.append('%s}' % ('  '*tab))
    return ''.join(s)

print format({'has': {'plants': 'yes', 'animals': 'yes', 'cryptonite': 'no'}, 'name': 'Earth'}})

Output:

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

Note that I'm sorta assuming all keys are strings, or at least pretty objects here

Knio
Works as expected. Anyway, I hope that somebody might find a all-batteries-included solution. If there is nothing else after the weekend, I use your solution. Thanks for your help!
erikb
+1  A: 

Depending on what you're doing with the output, one option is to use JSON for the display.

import json
x = {'planet' : {'has': {'plants': 'yes', 'animals': 'yes', 'cryptonite': 'no'}, 'name': 'Earth'}}

print json.dumps(x, indent=2)

Output:

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

The caveat to this approach is that some things are not serializable by JSON. Some extra code would be required if the dict contained non-serializable items like classes or functions.

David Narayan
Nice, never seen this json indent options :)
ohe
I would like to give you more then one up for this response. Just awesome! Thanks, man!
erikb