python - Pydot 不能很好地处理换行符?

标签 python png newline pydot

即时 - Ubuntu 12.04,python 2.7,使用 pydot 1.0.2

我正在尝试让 pydot 将树打印为 .png。我正在制作我的节点:

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

这里,cnfullstr 只是字符串。

这很好,但是当我运行时:

graph.write_png(output_path)

我收到以下错误(哎呀!)- 休息后继续关注

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

难道我失去了你吗?还和我在一起吗?

如果我删除换行符,它会完美运行 - 没有错误。

所以我的问题是,如果换行符导致它像这样乱七八糟,我怎么才能换行呢?为什么换行会导致这样的错误开始?

最佳答案

换行符 "\n"r"\n"(两个字符反斜杠和 n)之间存在差异。后者是 raw string , 并且对正则表达式特别有用。

至于为什么一个字符串有效而另一个无效:pydot 库是“愚蠢的”,它只是为外部 dot 服务重新格式化命令。命令中间的换行符会混淆它,从而导致“语法错误”和其他问题。

这是修改后的代码:

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

关于python - Pydot 不能很好地处理换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24442094/

相关文章:

delphi - 为什么这个用于淡化 PNG 图像的 D2006 代码不起作用?

Java JTextArea 多行帮助

c# - 流读取行

python - 在 0's and 1' 的矩阵中查找路径

python - python 中 if 和 else 的 namespace 理解

r - 如何在R中将html输出捕获为png

ruby-on-rails - RoR : How to get an empty/new line in a plain text email

python - 如何将*任何* Python 对象转换为字符串?

python - android中的丰富链接预览

javascript - 执行文档范围 PNG 动画的最有效方法