我自己不使用PyGraphViz,但是在Python中處理文本很簡單。鑑於這個問題,我已經叫gra1.txt
輸入文件,Python文件gr.py
如下:
import sys, subprocess
count = 0
for line in sys.stdin:
if line[0] == '0':
outf = "g%d" % (count)
g = "graph G%d {\n" % (count)
count += 1
elif line[0] == '1':
g += "}\n"
dot = subprocess.Popen(["dot", "-Tjpg", "-o%s.jpg" % outf],
stdin=subprocess.PIPE,universal_newlines=True)
print (g)
dot.communicate(g)
elif len(line.rstrip()) == 0:
pass
else:
first = line.rstrip()
rel = sys.stdin.readline()
last = sys.stdin.readline().rstrip()
g += "%s -- %s\n" % (first,last)
...命令python gra1.py <gra1.txt
產生輸出:
$ python gra1.py <gra1.txt
graph G0 {
A -- B
A -- C
B -- C
}
graph G1 {
A -- C
B -- C
}
...隨着文件g0.jpg
:

...和g1.jpg
:

你能給出你一個示例中找到更多的信息期望結果圖看起來像?我認爲第二個看起來像'A - C - B',第一個是三角形,A,B和C三角形。 – Simon
是的。第一個包含三個角落的A,B和C.第二個將包含A-C-B,即C與B和A都連接。 – abhisekG