Pydot not playing well with line breaks?

做~自己de王妃 提交于 2019-12-02 00:55:03

问题


Off the bat - Ubuntu 12.04, python 2.7, using pydot 1.0.2

I'm trying to get pydot to print a tree to a .png. I'm making my nodes like so:

subnode = pydot.Node(fullstr, label = c + "\n" + n)
graph.add_node(subnode)

Here, c and n and fullstr are just strings.

This is fine but when I run:

graph.write_png(output_path)

I get the following error (yikes!) - stay tuned after the break

Traceback (most recent call last):
  File "Ant.py", line 47, in <module>
    f.makeOutput("output/ant", display=True)
  File "/home/aespielberg/ppr/ppr/svggen/library/api/component.py", line 322, in makeOutput
    self.makeComponentTree(filedir + "/tree.png")
  File "/home/aespielberg/ppr/ppr/svggen/library/api/component.py", line 267, in makeComponentTree
    graph.write_png(fn)
  File "/usr/lib/pymodules/python2.7/pydot.py", line 1602, in <lambda>
    lambda path, f=frmt, prog=self.prog : self.write(path, format=f, prog=prog))
  File "/usr/lib/pymodules/python2.7/pydot.py", line 1696, in write
    dot_fd.write(self.create(prog, format))
  File "/usr/lib/pymodules/python2.7/pydot.py", line 1796, in create
    status, stderr_output) )
pydot.InvocationException: Program terminated with status: 1. stderr follows: Warning: /tmp/tmpU5z0G8:2: string ran past end of line
Warning: /tmp/tmpU5z0G8:4: string ran past end of line
Error: /tmp/tmpU5z0G8:5: syntax error near line 5
context: Root >>>  -- <<<  "Root/front";
Warning: /tmp/tmpU5z0G8:6: string ran past end of line
Warning: /tmp/tmpU5z0G8:8: string ran past end of line
Warning: /tmp/tmpU5z0G8:10: string ran past end of line
Warning: /tmp/tmpU5z0G8:12: string ran past end of line
Warning: /tmp/tmpU5z0G8:14: string ran past end of line
Warning: /tmp/tmpU5z0G8:16: string ran past end of line
Warning: /tmp/tmpU5z0G8:18: string ran past end of line
Warning: /tmp/tmpU5z0G8:20: string ran past end of line
Warning: /tmp/tmpU5z0G8:22: string ran past end of line
Warning: /tmp/tmpU5z0G8:24: string ran past end of line
Warning: /tmp/tmpU5z0G8:26: string ran past end of line
Warning: /tmp/tmpU5z0G8:28: string ran past end of line
Warning: /tmp/tmpU5z0G8:30: string ran past end of line
Warning: /tmp/tmpU5z0G8:32: string ran past end of line
Warning: /tmp/tmpU5z0G8:34: string ran past end of line
Warning: /tmp/tmpU5z0G8:36: string ran past end of line
Warning: /tmp/tmpU5z0G8:38: string ran past end of line
Warning: /tmp/tmpU5z0G8:40: string ran past end of line
Warning: /tmp/tmpU5z0G8:42: string ran past end of line
Warning: /tmp/tmpU5z0G8:44: string ran past end of line
Warning: /tmp/tmpU5z0G8:46: string ran past end of line
Warning: /tmp/tmpU5z0G8:48: string ran past end of line
Warning: /tmp/tmpU5z0G8:50: string ran past end of line
Warning: /tmp/tmpU5z0G8:52: string ran past end of line
Warning: /tmp/tmpU5z0G8:54: string ran past end of line
Warning: /tmp/tmpU5z0G8:56: string ran past end of line
Warning: /tmp/tmpU5z0G8:58: string ran past end of line
Warning: /tmp/tmpU5z0G8:60: string ran past end of line
Warning: /tmp/tmpU5z0G8:62: string ran past end of line
Warning: /tmp/tmpU5z0G8:64: string ran past end of line
Warning: /tmp/tmpU5z0G8:66: string ran past end of line
Warning: /tmp/tmpU5z0G8:68: string ran past end of line
Warning: /tmp/tmpU5z0G8:70: string ran past end of line
Warning: /tmp/tmpU5z0G8:72: string ran past end of line
Warning: /tmp/tmpU5z0G8:74: string ran past end of line
Warning: /tmp/tmpU5z0G8:76: string ran past end of line
Warning: /tmp/tmpU5z0G8:78: string ran past end of line
Warning: /tmp/tmpU5z0G8:80: string ran past end of line
Warning: /tmp/tmpU5z0G8:82: string ran past end of line
Warning: /tmp/tmpU5z0G8:84: string ran past end of line
Warning: /tmp/tmpU5z0G8:86: string ran past end of line
Warning: /tmp/tmpU5z0G8:88: string ran past end of line
Warning: /tmp/tmpU5z0G8:90: string ran past end of line
Warning: /tmp/tmpU5z0G8:92: string ran past end of line
Warning: /tmp/tmpU5z0G8:94: string ran past end of line
Warning: /tmp/tmpU5z0G8:96: string ran past end of line
Warning: /tmp/tmpU5z0G8:98: string ran past end of line
Warning: /tmp/tmpU5z0G8:100: string ran past end of line
Warning: /tmp/tmpU5z0G8:102: string ran past end of line
Warning: /tmp/tmpU5z0G8:104: string ran past end of line
Warning: /tmp/tmpU5z0G8:106: string ran past end of line
Warning: /tmp/tmpU5z0G8:108: string ran past end of line
Warning: /tmp/tmpU5z0G8:110: string ran past end of line
Warning: /tmp/tmpU5z0G8:112: string ran past end of line
Warning: /tmp/tmpU5z0G8:114: string ran past end of line
Warning: /tmp/tmpU5z0G8:116: string ran past end of line
Warning: /tmp/tmpU5z0G8:118: string ran past end of line
Warning: /tmp/tmpU5z0G8:120: string ran past end of line
Warning: /tmp/tmpU5z0G8:122: string ran past end of line
Warning: /tmp/tmpU5z0G8:124: string ran past end of line
Warning: /tmp/tmpU5z0G8:126: string ran past end of line
Warning: /tmp/tmpU5z0G8:128: string ran past end of line
Warning: /tmp/tmpU5z0G8:130: string ran past end of line
Warning: /tmp/tmpU5z0G8:132: string ran past end of line
Warning: /tmp/tmpU5z0G8:134: string ran past end of line
Warning: /tmp/tmpU5z0G8:136: string ran past end of line
Warning: /tmp/tmpU5z0G8:138: string ran past end of line
Warning: /tmp/tmpU5z0G8:140: string ran past end of line
Warning: /tmp/tmpU5z0G8:142: string ran past end of line
Warning: /tmp/tmpU5z0G8:144: string ran past end of line
Warning: /tmp/tmpU5z0G8:146: string ran past end of line
Warning: /tmp/tmpU5z0G8:148: string ran past end of line
Warning: /tmp/tmpU5z0G8:150: string ran past end of line
Warning: /tmp/tmpU5z0G8:152: string ran past end of line
Warning: /tmp/tmpU5z0G8:154: string ran past end of line
Warning: /tmp/tmpU5z0G8:156: string ran past end of line
Warning: /tmp/tmpU5z0G8:158: string ran past end of line
Warning: /tmp/tmpU5z0G8:160: string ran past end of line
Warning: /tmp/tmpU5z0G8:162: string ran past end of line
Warning: /tmp/tmpU5z0G8:164: string ran past end of line
Warning: /tmp/tmpU5z0G8:166: string ran past end of line
Warning: /tmp/tmpU5z0G8:168: string ran past end of line

Did I lose you in all that? Still with me?

If I remove the line break, it works perfectly - no errors.

So my question is, how can I get line breaks if the line break character causes it to mess up like this? And why would a line break cause such an error to begin with?


回答1:


There's a difference between "\n", a newline, vs r"\n", the two characters backslash and n. The latter is a raw string, and is especially useful for regular expressions.

As to why one string works and the other doesn't: the pydot library is "dumb", it's just reformatting commands for an external dot service. The newline in the middle of commands to dot confuses it, thus the "syntax error" and other issues.

Here's the patched code:

subnode = pydot.Node(fullstr, label = c + r"\n" + n)
graph.add_node(subnode)


来源:https://stackoverflow.com/questions/24442094/pydot-not-playing-well-with-line-breaks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!