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

Python脚本开发需求:提取电源网络文件中M9的指定电压降值

提取电源网络M9行最后数值的Python实现方案
  • 核心思路:逐行遍历文件,通过状态标记控制数据读取——当遇到Layer/Via#时进入"跳过模式",直到碰到Layer/Via时退出;进入表格区域后,匹配M9开头的行,提取最后一个数值。

  • 示例代码实现:

def extract_m9_values(file_path):
    result = {}
    current_net = None
    skip_mode = False
    in_table = False

    with open(file_path, 'r') as f:
        for line in f:
            line = line.strip()
            if not line:
                continue
            
            # 识别当前网络名称
            if line.startswith('Net Name:'):
                current_net = line.split(':')[-1].strip()
                result[current_net] = None
                continue
            
            # 进入跳过模式
            if line == 'Layer/Via#':
                skip_mode = True
                continue
            
            # 退出跳过模式,准备读取表格
            if line == 'Layer/Via':
                skip_mode = False
                in_table = True
                continue
            
            # 跳过不需要的行
            if skip_mode:
                continue
            
            # 在表格中查找M9行
            if in_table:
                # 表格结束判断(可根据实际文件格式调整)
                if not line.startswith(('M', 'V')):
                    in_table = False
                    continue
                if line.startswith('M9'):
                    parts = line.split()
                    if parts:
                        result[current_net] = parts[-1]
    
    return result

# 使用示例
output = extract_m9_values('power_nets.txt')
for net, value in output.items():
    print(f"{net}: {value}")
  • 代码说明:
    • current_net跟踪当前处理的电源网络名称
    • skip_mode标记是否跳过Layer/Via#至Layer/Via之间的内容
    • in_table标记是否处于Layer/Via表格数据区域
    • 匹配M9行后,通过split()拆分文本,取最后一个元素即为目标数值
    • 若文件用制表符分隔表格,可将split()替换为split('\t')提升精准度

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 02:50:40