Search code examples
pythonpython-2.7graphvizanacondapydot

Pydot Error involving parsing ':' character followed by number


So I was using pydot in python 2.7 from Anaconda and noticed I keep getting errors when I attempt to use certain strings in Pydot.

The error I have isolated to:

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)

Whenever I run this I get the following output:

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 So Far:

Somehow the combination of a colon character ':' followed by a number in str() format seems to raise an error. I tried to fix it by append the 'r' in front since I know that is a way to fix errors invlving the '\n' character. But even then no luck.

Changes:

I removed the r as it appears to be causing a little confusion. I had kept r':' hoping to emulate the solution to the problem of non compiling newlines '\n', since pydot requires them to be listed as r'\n' where r is explicitly not defined.

As per:

Pydot not playing well with line breaks?


Solution

  • I found this issue number 38 - Which says we cannot use special symbols (like colon) in node names or labels. The reason it has highlighted is -

    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:###@"'
    

    Though you may be better off not having colon in your name.