parse multiple digraph in one dot file
I'm trying to process and render some graph in DOT format. The dot file I have is large (~300MB), and it contains multiple digraphs
digraph 1 {...}
digraph 2 {...}
digraph 3 {...}
I ha开发者_如何学Cve got 2 questions:
1. Is it possible to use render only 1 digraph instead of the whole graph? Something likedot -3 -Tps mygraph.dot -o out.ps
to render digraph 3 only?
2.What's the best Python library to process dot format?(other languages are also acceptable)
Here are two I tried, but not good enough pydot It gives me a digraph list after importing, which is good, but it doesn't handle "." in the node name. For examplend.nd [label="nd_node"]
will fail
pygraphviz It does handle ".", but only imports digraph 1 when given multiple graph definition in a file :(
Since you have dot
, you should also have gvpr
, a graph-processing tool. You can print the third graph with a simple gvpr
script, like so:
BEGIN { int count = 0; }
BEG_G {
count = count + 1;
if(count == 3) {
write($G);
}
}
Then, you can use this as a source filter:
cat mygraph.dot | gvpr -f thirdgraph.gv | dot -Tps -o out.ps
Not sure if this is what you wanted, but you just need to write a simple parser, or whatever you want to call it. The pygraphviz handles file input by simply read and parse as it would for string input.
So, a sample piece of code would be:
f = open(filename, 'r')
graph = [[]]
while True:
line = f.readline()
if not line:
break
if declare_new_graph(line):
graph.append([])
graph[-1].append(line)
#now you have a list of graphs
#each is a list of lines
#with first line containing the name
#to get the string representation of graph "i"
str = '\n'.join(graph[i])
精彩评论