Pydot在换行符上玩的不好吗? [英] Pydot not playing well with line breaks?

查看:86
本文介绍了Pydot在换行符上玩的不好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我正在尝试使pydot将树打印为.png.我正在像这样制作节点:

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)

在这里,cnfullstr只是字符串.

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?

推荐答案

"\n",换行符和r"\n",两个字符反斜杠和n之间是有区别的.后者是原始字符串,对于正则表达式特别有用.

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.

关于一个字符串为什么起作用而另一个字符串不起作用的原因:pydot库是哑巴",它只是重新格式化外部dot服务的命令.命令中间的换行符将其弄乱,从而引起语法错误"和其他问题.

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.

这是修补后的代码:

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

这篇关于Pydot在换行符上玩的不好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆