Printing dictionaries (JSON) human readable

て烟熏妆下的殇ゞ 提交于 2019-12-10 17:22:59

问题


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.


回答1:


This is adapted from a activestate code example. Can't say its pretty, but may get you headed in the right direction:

myDict = {'level1':
         {'level2':(1, 2),
          'level3':
                [
                 {'level4': (1, 2)},
                 {'level5': (1, 2)},
                ], 
         'level6': [1,2,3], 
         'level7':{'level8': (1,2), 'level9': (1,2)}
         }
    }



def prettyPrint(dictionary, ident = '', braces=1):
    for key, value in dictionary.iteritems():
        if isinstance(value, dict):
            print '%s%s%s%s' % (ident, braces*'[', key, braces*']') 
            prettyPrint(value, ident+'  ', braces+1)
        elif isinstance(value, list):
            ndict=0
            for v in value:
                if isinstance(v, dict):
                    ndict += 1
            if ndict:
                print '%s%s' % (ident, key) 
                for e in value:
                    if isinstance(e, dict):
                        prettyPrint(e, ident+'  ', braces+1)
                    else: 
                         print ident+'%s : %s' %(key, e)
            else:
                print ident+'%s : %s' %(key, value)
        else:
            print ident+'%s : %s' %(key, value)

prettyPrint(myDict)


[level1]
  level2 : (1, 2)
  level3
    level4 : (1, 2)
    level5 : (1, 2)
  level6 : [1, 2, 3]
  [[level7]]
    level8 : (1, 2)
    level9 : (1, 2)


来源:https://stackoverflow.com/questions/15275766/printing-dictionaries-json-human-readable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!