如何从树点文件制作表格?

发布于 2025-01-13 08:04:19 字数 469 浏览 4 评论 0原文

如何从树点文件制作表格?

例如:点文件中的这一行:

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 ;

所以表:

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

我如何在Python中制作这个表?

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??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情旳空白 2025-01-20 08:04:19

如果您想使用自己的代码执行此操作,则应用正则表达式来分离文件的每一行非常简单。下面是一个为您的输入提供所需结果的示例:

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()))

结果:

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

我不熟悉这种文件格式,因此我不知道您是否需要比此表达式更复杂的表达式来处理所有可能的有效输入。如果上述表达式不适用于您想要映射到结果表的所有可能的行,您应该能够调整表达式以获得您想要的行为。

如果有一个包可以为您执行此操作,这样您就不必了解文件格式的详细信息,那么使用它显然是一个更干净的解决方案。我不熟悉这个特定的问题领域,所以我不会告诉你这样的事情是否可能存在。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文