'how to make a table from a tree dot file?

how to make a table from a tree dot file??

for example: this lines from dot file :

0 [label="TV <= -0.239\nmse = 25.8\nsamples = 160\nvalue = 14.218"] ;

1 [label="TV <= -1.422\nmse = 7.824\nsamples = 66\nvalue = 10.015"] ;

0 -> 1 [labeldistance=2.5, labelangle=45, headlabel="True"] ;

2 [label="radio <= 0.549\nmse = 2.58\nsamples = 19\nvalue = 6.805"] ;
1 -> 2 ;

so the table:

0,TV,-0.239
1,TV,-1.422
2,radio,0.549
.
.
.

how can I make this table in python??



Solution 1:[1]

If you're looking to do this with your own code, applying a regular expression to pick apart each line of the file is straightforward. Here's an example that gives the desired result for your input:

import re

pat = re.compile(r'^(\d+).*?\[label="(\S+)\s+<=\s+(\S+?)\\n')

with open('graph.dot') as f:
    for line in f:
        m = pat.match(line)
        if m:
            print(",".join(m.groups()))

Result:

0,TV,-0.239
1,TV,-1.422
2,radio,0.549

I'm not familiar with this file format, so I don't know if you'd need a more sophisticated expression than this one to handle all possible valid inputs. If the above expression doesn't work for all possible lines that you want to map to the resulting table, you should be able to tweak the expression to get the behavior you desire.

If there is a package that will do this for you so that you don't have to understand the details of the file format, using that would obviously be a cleaner solution. I'm not familiar with this particular problem domain, so I'm not one to tell you if such a thing might exist.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CryptoFool