涉及解析“:”字符后跟数字的 Pydot 错误

Pydot Error involving parsing ':' character followed by number

所以我在 Anaconda 的 python 2.7 中使用 pydot,并注意到当我尝试在 Pydot 中使用某些字符串时,我总是遇到错误。

我查出的错误:

import pydot


graph = pydot.Dot(graph_type='digraph', rankdir = 'LR')
S = 'Total Flow Count ' + ':' + str(3)
legend = pydot.Node('Legend', label=S, shape='rectangle')

graph.add_node(legend)

每当我 运行 这个我得到以下输出:

Traceback (most recent call last):
  File "path\of\my\code\errorisolate.py", line 13, in <module>
    graph.write_png('example5graph.png')
  File "c:\Anaconda\lib\site-packages\pydot.py", line 1609, in <lambda>
    lambda path, f=frmt, prog=self.prog : self.write(path, format=f, prog=prog))
  File "c:\Anaconda\lib\site-packages\pydot.py", line 1703, in write
dot_fd.write(self.create(prog, format))
  File "c:\Anaconda\lib\site-packages\pydot.py", line 1803, in create
status, stderr_output) )
InvocationException: Program terminated with status: 6. stderr follows: Error: c:\users\sidharth\appdata\local\temp\tmpxvwsls:3: syntax error near line 3

context: Legend [shape=rectangle, label=Total Flow Count >>>  : <<< 3];

Analysis/Work 到目前为止:

冒号字符“:”后跟 str() 格式的数字的组合似乎会引发错误。我试图通过在前面附加 'r' 来修复它,因为我知道这是修复涉及 '\n' 字符的错误的方法。但即便如此也没有运气。

更改:

我删除了 r,因为它看起来有点混乱。我一直保留 r':' 希望模拟非编译换行符 '\n' 问题的解决方案,因为 pydot 要求将它们列为 r'\n',其中 r 未明确定义。

根据:

Pydot not playing well with line breaks?

我发现了这个 issue number 38 - 它说我们不能在节点名称或标签中使用特殊符号(如冒号)。它突出显示的原因是 -

As with issue 28: The problem with the colon in Node names is that Graphviz will use them to specify a port where to attach edges, it's a Graphviz artifact. The way pydot supports them is to allow them in names, if you wish to simply have colon characters in the name simply add quotes to the string.

For instance: (note the double quotes in the actual string):

node = pydot.Node('"Testnode:###@"')

print node.get_name()
'"Testnode:###@"'

虽然你的名字中没有冒号可能会更好。