Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 漂亮的打印名称_Python_Namedtuple_Pprint - Fatal编程技术网

Python 漂亮的打印名称

Python 漂亮的打印名称,python,namedtuple,pprint,Python,Namedtuple,Pprint,我从pprint尝试了pprint,但它的输出只有一行,没有多行输出,也没有缩进。我使用内置函数将命名的元组作为字典来获取 但是,它返回一个OrderedDict,而pprint不会缩进,因此我将其转换为dict: >>> from collections import namedtuple >>> Busbar = namedtuple('Busbar', 'id name voltage') >>> busbar = Busbar(id

我从
pprint
尝试了
pprint
,但它的输出只有一行,没有多行输出,也没有缩进。

我使用内置函数将命名的元组作为字典来获取

但是,它返回一个
OrderedDict
,而
pprint
不会缩进,因此我将其转换为
dict

>>> from collections import namedtuple

>>> Busbar = namedtuple('Busbar', 'id name voltage')
>>> busbar = Busbar(id=102, name='FACTORY', voltage=21.8)
>>> from pprint import pprint
>>> pprint(dict(vars(busbar)))
{'id': 102,
 'name': 'FACTORY',
 'voltage': 21.8}
使用
pprint
dict

>>> from collections import namedtuple

>>> Busbar = namedtuple('Busbar', 'id name voltage')
>>> busbar = Busbar(id=102, name='FACTORY', voltage=21.8)
>>> from pprint import pprint
>>> pprint(dict(vars(busbar)))
{'id': 102,
 'name': 'FACTORY',
 'voltage': 21.8}

Python 3中的pprint PrettyPrinter比Python 2中的pprint PrettyPrinter更具扩展性。您可以像下面这样创建自己的打印机,为要处理的对象添加方法,而不会与pprint“private”方法和属性过多混淆

您可以在此处看到一个在线示例:

并像这样使用它:

from collections import namedtuple

Segment = namedtuple('Segment', 'p1 p2')
# Your own namedtuple-like class
class Node:
    def __init__(self, x, y, segments=[]):
        self.x = x
        self.y = y
        self.segments = segments

    def _asdict(self):
        return {"x": self.x, "y": self.y, "segments": self.segments}

    # Default repr
    def __repr__(self):
        return "Node(x={}, y={}, segments={})".format(self.x, self.y, self.segments)

# A circular structure for the demo
node = Node(0, 0)
segments = [
    Segment(node, Node(1, 1)),
    Segment(node, Node(2, 1)),
    Segment(node, Node(1, 2, segments=[
      Segment(Node(2, 3), Node(1, 1)),
    ])),
]
node.segments = segments

pp = MyPrettyPrinter(indent=2, depth=2)
pp.pprint(node)
输出

Node(
  x=0,
  y=0,
  segments=[ Segment(
                p1=<Recursion on Node with id=139778851454536>,
                p2=Node(x=1, y=1, segments=[])),
              Segment(
                p1=<Recursion on Node with id=139778851454536>,
                p2=Node(x=2, y=1, segments=[])),
              Segment(
                p1=<Recursion on Node with id=139778851454536>,
                p2=Node(x=1, y=2, segments=[...]))])
节点(
x=0,
y=0,
段=[段](
p1=,
p2=节点(x=1,y=1,段=[]),
分段(
p1=,
p2=节点(x=2,y=1,段=[]),
分段(
p1=,
p2=节点(x=1,y=2,段=[…]))

与此处的所有其他解决方案不同,此解决方案是通用的,并且也适用于其他容器中的namedtuples:

import black

# value_to_print can either be a namedtuple, or a container containing tuples,
# or a namedtuple containing containers containing other namedtuples,
# or whatever else you want.
print(black.format_str(repr(value_to_print), mode=black.Mode()))

这需要安装black,这可以通过
sudo pip install black

完成。您能给出一个要打印的对象的示例,以及打印输出的显示方式吗?您期望的是什么?如果您需要更多地控制它的打印方式,请创建一个自定义对象并定义
\uuuu repr\uuuu
@KFL,这样做行不通,不行。您必须编写一个小的实用函数。