2

我想pprint用来打印一些复杂的列表,见下​​面的例子:

>>> x = [OrderedDict([(u'D-ID', u'clci-2'), (u'R', u'1691')]), OrderedDict([(u'D-ID', u'dallas-pcc-1'), (u'R', u'5498')]), OrderedDict([(u'D-ID', u'gx-1'), (u'R', u'2275')]), OrderedDict([(u'D-ID', u'gx-2'), (u'R', u'0')]), OrderedDict([(u'D-ID', u'ocs-1'), (u'R', u'31735')]), OrderedDict([(u'D-ID', u'ocs-2'), (u'R', u'0')]), OrderedDict([(u'D-ID', u'pcc-gx-1'), (u'R', u'46351')]), OrderedDict([(u'D-ID', u'rgx-1'), (u'R', u'907')]), OrderedDict([(u'D-ID', u'rgx-2'), (u'R', u'1010')]), OrderedDict([(u'D-ID', u'rocs-1'), (u'R', u'915')]), OrderedDict([(u'D-ID', u'rocs-2'), (u'R', u'1033')])]
>>> pprint.pprint(x)
[OrderedDict([(u'D-ID', u'clci-2'), (u'R', u'1691')]),
 OrderedDict([(u'D-ID', u'dallas-pcc-1'), (u'R', u'5498')]),
 OrderedDict([(u'D-ID', u'gx-1'), (u'R', u'2275')]),
 OrderedDict([(u'D-ID', u'gx-2'), (u'R', u'0')]),
 OrderedDict([(u'D-ID', u'ocs-1'), (u'R', u'31735')]),
 OrderedDict([(u'D-ID', u'ocs-2'), (u'R', u'0')]),
 OrderedDict([(u'D-ID', u'pcc-gx-1'), (u'R', u'46351')]),
 OrderedDict([(u'D-ID', u'rgx-1'), (u'R', u'907')]),
 OrderedDict([(u'D-ID', u'rgx-2'), (u'R', u'1010')]),
 OrderedDict([(u'D-ID', u'rocs-1'), (u'R', u'915')]),
 OrderedDict([(u'D-ID', u'rocs-2'), (u'R', u'1033')])]

这很好,除了我想删除所有类型 name( OrderedDict & u) 和所有不必要[]的 . pprint可以吗?

4

3 回答 3

2

您需要创建自己的 class 子pprint.PrettyPrinter,并覆盖该.format()方法:

from pprint import PrettyPrinter, _recursion

class MyPrettyPrinter(PrettyPrinter):
    def format(self, object, context, maxlevels, level):
        if isinstance(object, OrderedDict):
            if not object:
                return "()", True, False

            objid = id(object)
            if maxlevels and level >= maxlevels:
                return "(...)", False, objid in context
            if objid in context:
                return _recursion(object), False, True
            context[objid] = 1
            readable = True
            recursive = False
            components = []
            append = components.append
            level += 1
            selfformat = self.format
            for k, v in sorted(object.items()):
                krepr, kreadable, krecur = selfformat(k, context, maxlevels, level)
                vrepr, vreadable, vrecur = selfformat(v, context, maxlevels, level)
                append('({}, {})'.format(krepr, vrepr))
                readable = readable and kreadable and vreadable
                if krecur or vrecur:
                    recursive = True
            del context[objid]
            return '({})'.format(', '.join(components)), readable, recursive

        formatted, readable, recursion = PrettyPrinter.format(self, object, context, maxlevels, level)
        if isinstance(object, unicode):
            formatted = formatted.lstrip('u')

        return formatted, readable, recursion

我们让基类完成大部分工作,并且只OrderedDict显式地格式化;它有一个自定义__repr__,这意味着原始pprint实现只是使用它而不是递归它。

然后使用此类而不是直接调用pprint.pprint()

mypprint = MyPrettyPrinter()

mypprint.pprint(x)

这会产生:

>>> mypprint = MyPrettyPrinter()
>>> mypprint.pprint(x)
[(('D-ID', 'clci-2'), ('R', '1691')),
 (('D-ID', 'dallas-pcc-1'), ('R', '5498')),
 (('D-ID', 'gx-1'), ('R', '2275')),
 (('D-ID', 'gx-2'), ('R', '0')),
 (('D-ID', 'ocs-1'), ('R', '31735')),
 (('D-ID', 'ocs-2'), ('R', '0')),
 (('D-ID', 'pcc-gx-1'), ('R', '46351')),
 (('D-ID', 'rgx-1'), ('R', '907')),
 (('D-ID', 'rgx-2'), ('R', '1010')),
 (('D-ID', 'rocs-1'), ('R', '915')),
 (('D-ID', 'rocs-2'), ('R', '1033'))]
于 2013-04-09T11:31:30.523 回答
0
>>> import pprint
>>> from collections import OrderedDict
>>> x = [OrderedDict([(u'D-ID', u'clci-2'), (u'R', u'1691')]), OrderedDict([(u'D-ID', u'dallas-pcc-1'), (u'R', u'5498')]), OrderedDict([(u'D-ID', u'gx-1'), (u'R', u'2275')]), OrderedDict([(u'D-ID', u'gx-2'), (u'R', u'0')]), OrderedDict([(u'D-ID', u'ocs-1'), (u'R', u'31735')]), OrderedDict([(u'D-ID', u'ocs-2'), (u'R', u'0')]), OrderedDict([(u'D-ID', u'pcc-gx-1'), (u'R', u'46351')]), OrderedDict([(u'D-ID', u'rgx-1'), (u'R', u'907')]), OrderedDict([(u'D-ID', u'rgx-2'), (u'R', u'1010')]), OrderedDict([(u'D-ID', u'rocs-1'), (u'R', u'915')]), OrderedDict([(u'D-ID', u'rocs-2'), (u'R', u'1033')])]
>>> print pprint.pformat(x).replace("OrderedDict([", "(") \
                           .replace("u'", "'") \
                           .replace("])", ")")

[(('D-ID', 'clci-2'), ('R', '1691')),
 (('D-ID', 'dallas-pcc-1'), ('R', '5498')),
 (('D-ID', 'gx-1'), ('R', '2275')),
 (('D-ID', 'gx-2'), ('R', '0')),
 (('D-ID', 'ocs-1'), ('R', '31735')),
 (('D-ID', 'ocs-2'), ('R', '0')),
 (('D-ID', 'pcc-gx-1'), ('R', '46351')),
 (('D-ID', 'rgx-1'), ('R', '907')),
 (('D-ID', 'rgx-2'), ('R', '1010')),
 (('D-ID', 'rocs-1'), ('R', '915')),
 (('D-ID', 'rocs-2'), ('R', '1033'))]

replace根据您的需要调整s,我认为这看起来足够干净

于 2013-04-09T11:04:59.180 回答
0

If it's just for printing some dict and list stuff (or stuff from classes inheriting from them), you would be better off using some other serialization, e.g., JSON:

import json
print json.dumps(obj)
于 2013-04-09T12:53:01.613 回答