How to convert text file automatically to graphviz dot file?
Asked Answered
G

2

2

I am trying to convert my text file to an undirected graph automatically with the help of graphviz. The text file consists of the following code:

0

A
Relation
B
A
Relation
C
B
Relation
C
1

0

A
Relation
C

B
Relation
C
1

Here A, B and C are nodes. I may require a single or multiple graphs. 0 and 1 represent the start and end of each graph. The number of relations may also vary. I tried to proceed with sed, but got lost. How should I proceed to get the graph I require? Thanks for your help.

Gloriane answered 12/1, 2014 at 16:58 Comment(2)
Could you give an example of what you expect the resulting graphs to look like? Am I correct in thinking that the second one would look something like A -- C -- B and the first would be a triangle with A, B and C in the three corners.Naphthyl
yes exactly. The first would contain A, B and C in three corners. The second would contain A--C--B i.e. C is connected with B and A both.Gloriane
N
3

I don't use PyGraphViz myself, but doing the text processing in Python is easy enough. Given the input file in the question, which I've called gra1.txt, and a Python file gr.py as follows:

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)

... the command python gra1.py <gra1.txt produces the output:

$ python gra1.py <gra1.txt
graph G0 {
A -- B
A -- C
B -- C
}

graph G1 {
A -- C
B -- C
}

... along with the files g0.jpg:

enter image description here

... and g1.jpg:

enter image description here

Naphthyl answered 13/1, 2014 at 8:26 Comment(0)
C
1

You can do it with the graphviz python library. To install it you just need to launch:

pip install graphviz

and then in Python you can do:

from graphviz import Source

text_from_file = str()
with open('graphviz_dot_file.txt') as file:
    text_from_file = file.read()

src = Source(text_from_file)
src.render(test.gv', view=True ) 

You can find more informations in the Graphviz's manual

Cementite answered 29/6, 2016 at 8:25 Comment(2)
I got the error while trying your solution, PUN-U531305L001:Desktop 531305$ python graph.py Traceback (most recent call last): File "graph.py", line 8, in <module> src.render('test.gv', view=True) File "/Library/Python/2.7/site-packages/graphviz/files.py", line 226, in render 'are on your systems\' path' % cmd) RuntimeError: failed to execute ['dot', '-Tpdf', '-O', 'test.gv'], make sure the Graphviz executables are on your systems' pathSelfish
I had the similar issue. It's seem be you use Graphviz on Widows. Make sure you have Graphviz on your System path variables. Here your solutionCementite

© 2022 - 2024 — McMap. All rights reserved.