【问题标题】:how to check if a node is already present in Graphviz Python如何检查 Graphviz Python 中是否已经存在节点
【发布时间】:2020-04-29 15:38:37
【问题描述】:

如何检查图表中是否已经存在特定节点。

这是我尝试过的。

>>>from graphviz import Digraph
>>>dot = Digraph()
>>>dot.node('solid',xlabel='0')
>>>dot.node('liquid',xlabel='0')
>>>dot.edge('solid','liquid','melt')
>>>print (dot)

digraph {
    solid [xlabel=0]
    liquid [xlabel=0]
    solid -> liquid [label=melt]
}

>>>check = 'solid' in dot
>>>print (solid)

False

正如我们在这里看到的,我无法直接检查节点。

我正在寻找一种方法来检查该节点之前是否已被访问/创建过,如果该节点被访问过,则将其 xlabel 增加 1。

有没有办法遍历和访问graphviz中的每个节点,还是我必须编写单独的代码来检查特定节点是否存在?

【问题讨论】:

    标签: python graphviz dot


    【解决方案1】:

    显然 .body 属性包含一个带有以 tab 为前缀的节点的列表。你应该这样做:

    >>>print('\tsolid' in dot.body)
    
    True
    

    【讨论】:

      【解决方案2】:

      2020年对我有用的方法是:

      if '\tsolid' in dot.source:
          # Here you need to get the line number and know the real xlabel
          dot.body[line_number] = '\tsolid [xlabel='+str(real_xlabel)+']'
      

      【讨论】:

        猜你喜欢
        • 2019-08-21
        • 1970-01-01
        • 1970-01-01
        • 2021-05-15
        • 1970-01-01
        • 1970-01-01
        • 2016-03-29
        • 2019-03-19
        相关资源
        最近更新 更多