You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python提取dot文件中含指定标签的边与路径?

提取Dot文件中含特定标签的边

首次处理Dot文件,目标是提取其中包含特定标签(如示例中的V1)的路径,使用Python的pydot库时未能完成标签提取环节。以下是具体的示例文件、现有代码、期望输出及解决方案:

示例Dot文件

digraph "MVICFG" {
    label="MVICFG";
/* Generating Nodes */
    subgraph cluster_1 {
        label="main";
        "6" [label="4294967294::Entry::main"];
        "2" [label="0::  %1 = alloca i32, align 4"];
        "3" [label="0::  store i32 0, i32* %1, align 4"];
        "4" [label="0::  %2 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i32 0, i32 0)), !dbg !13"];
        "5" [label="4::  ret i32 0, !dbg !14"];
        "7" [label="4294967293::Exit::main"];
        "11" [label="3::  %1 = alloca i32, align 4"];
        "12" [label="3::  store i32 0, i32* %1, align 4"];
        "13" [label="3::  %2 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i32 0, i32 0)), !dbg !13"];
    }
    subgraph cluster_9 {
        label="External_Node_Func";
        "10" [label="4294967294::External_Node"];
    }

/* Generating Edges */
        "2" -> "3" [arrowhead = normal, penwidth = 1.0, color = black, label="V1"];
        "3" -> "4" [arrowhead = normal, penwidth = 1.0, color = black, label="V1"];
        "6" -> "2" [arrowhead = normal, penwidth = 1.0, color = pink, label="V1::Virtual"];
        "5" -> "7" [arrowhead = normal, penwidth = 1.0, color = pink, label="V1,V2::Virtual"];
        "4" -> "5" [arrowhead = normal, penwidth = 1.0, color = black, label="V1"];
        "6" -> "11" [arrowhead = normal, penwidth = 1.0, color = pink, label="V2::Virtual"];
        "13" -> "5" [arrowhead = normal, penwidth = 1.0, color = black, label="V2"];
        "11" -> "12" [arrowhead = normal, penwidth = 1.0, color = black, label="V2"];
        "12" -> "13" [arrowhead = normal, penwidth = 1.0, color = black, label="V2"];
}

现有Python代码

import pydot

dot_string = """digraph "MVICFG" {
    label="MVICFG";
/* Generating Nodes */
    subgraph cluster_1 {
        label="main";
        "6" [label="4294967294::Entry::main"];
        "2" [label="0::  %1 = alloca i32, align 4"];
        "3" [label="0::  store i32 0, i32* %1, align 4"];
        "4" [label="0::  %2 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i32 0, i32 0)), !dbg !13"];
        "5" [label="4::  ret i32 0, !dbg !14"];
        "7" [label="4294967293::Exit::main"];
        "11" [label="3::  %1 = alloca i32, align 4"];
        "12" [label="3::  store i32 0, i32* %1, align 4"];
        "13" [label="3::  %2 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i32 0, i32 0)), !dbg !13"];
    }
    subgraph cluster_9 {
        label="External_Node_Func";
        "10" [label="4294967294::External_Node"];
    }

/* Generating Edges */
        "2" -> "3" [arrowhead = normal, penwidth = 1.0, color = black, label="V1"];
        "3" -> "4" [arrowhead = normal, penwidth = 1.0, color = black, label="V1"];
        "6" -> "2" [arrowhead = normal, penwidth = 1.0, color = pink, label="V1::Virtual"];
        "5" -> "7" [arrowhead = normal, penwidth = 1.0, color = pink, label="V1,V2::Virtual"];
        "4" -> "5" [arrowhead = normal, penwidth = 1.0, color = black, label="V1"];
        "6" -> "11" [arrowhead = normal, penwidth = 1.0, color = pink, label="V2::Virtual"];
        "13" -> "5" [arrowhead = normal, penwidth = 1.0, color = black, label="V2"];
        "11" -> "12" [arrowhead = normal, penwidth = 1.0, color = black, label="V2"];
        "12" -> "13" [arrowhead = normal, penwidth = 1.0, color = black, label="V2"];
}

"""

graphs = pydot.graph_from_dot_data(dot_string)
graph = graphs[0]

期望输出

提取标签为V1的边,输出格式如下:

"2" -> "3" 
"3" -> "4" 
"4" -> "5" 

解决方案

在现有代码基础上,添加遍历图中所有边、筛选符合标签条件的逻辑,最终输出目标边:

import pydot

dot_string = """digraph "MVICFG" {
    label="MVICFG";
/* Generating Nodes */
    subgraph cluster_1 {
        label="main";
        "6" [label="4294967294::Entry::main"];
        "2" [label="0::  %1 = alloca i32, align 4"];
        "3" [label="0::  store i32 0, i32* %1, align 4"];
        "4" [label="0::  %2 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i32 0, i32 0)), !dbg !13"];
        "5" [label="4::  ret i32 0, !dbg !14"];
        "7" [label="4294967293::Exit::main"];
        "11" [label="3::  %1 = alloca i32, align 4"];
        "12" [label="3::  store i32 0, i32* %1, align 4"];
        "13" [label="3::  %2 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i32 0, i32 0)), !dbg !13"];
    }
    subgraph cluster_9 {
        label="External_Node_Func";
        "10" [label="4294967294::External_Node"];
    }

/* Generating Edges */
        "2" -> "3" [arrowhead = normal, penwidth = 1.0, color = black, label="V1"];
        "3" -> "4" [arrowhead = normal, penwidth = 1.0, color = black, label="V1"];
        "6" -> "2" [arrowhead = normal, penwidth = 1.0, color = pink, label="V1::Virtual"];
        "5" -> "7" [arrowhead = normal, penwidth = 1.0, color = pink, label="V1,V2::Virtual"];
        "4" -> "5" [arrowhead = normal, penwidth = 1.0, color = black, label="V1"];
        "6" -> "11" [arrowhead = normal, penwidth = 1.0, color = pink, label="V2::Virtual"];
        "13" -> "5" [arrowhead = normal, penwidth = 1.0, color = black, label="V2"];
        "11" -> "12" [arrowhead = normal, penwidth = 1.0, color = black, label="V2"];
        "12" -> "13" [arrowhead = normal, penwidth = 1.0, color = black, label="V2"];
}

"""

graphs = pydot.graph_from_dot_data(dot_string)
graph = graphs[0]

# 筛选标签严格为"V1"的边
target_label = '"V1"'
filtered_edges = []
for edge in graph.get_edges():
    edge_label = edge.get('label')
    if edge_label == target_label:
        # 按期望格式拼接边的描述
        edge_str = f'"{edge.get_source()}" -> "{edge.get_destination()}"'
        filtered_edges.append(edge_str)

# 输出结果
for line in filtered_edges:
    print(line)

运行上述代码后,即可得到符合要求的输出。

内容的提问来源于stack exchange,提问作者desert_ranger

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 10:30:50