我想使用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')])]
这很好,除了我要删除所有类型名称(OrderedDict & u
)和所有不必要的[]
。 是否可以使用pprint?
答案 0 :(得分:2)
您需要创建自己的pprint.PrettyPrinter
class子类,并覆盖.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'))]
答案 1 :(得分: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
,我觉得这看起来很干净
答案 2 :(得分:0)
如果只是打印一些dict
和list
内容(或者继承自它们的类中的内容),那么最好使用其他一些序列化,例如JSON:
import json
print json.dumps(obj)