打印人类可读的字典 (JSON) [英] Printing dictionaries (JSON) human readable

查看:62
本文介绍了打印人类可读的字典 (JSON)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个这样的(嵌套)字典(注意列表值):

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)}
                    ]
             }
       }

我正在寻找一种正确的方法来打印这本字典,我正在使用 json 来做到这一点:

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
                ]
            }
        ]
    }
}

虽然上面的输出很不错,但还是很难阅读,特别是如果有很多级别和较长的名称.我也试过 yaml

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

我相信上面的内容更容易阅读,并且可能有 python 库可以做到这一点.

I believe the above is much easier to read and there may be python libraries out there that can do this.

推荐答案

这是改编自一个 activestate 代码示例.不能说它很漂亮,但可能会让你朝着正确的方向前进:

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)

这篇关于打印人类可读的字典 (JSON)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆