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

Python读取txt文件时列表未追加数据、缺失字典键问题求解

问题原因分析

  • 缺失Section_B条目:原有代码大概率仅识别到首个Section(Section_A)后就终止了Section遍历逻辑,没有继续读取后续Section对应的行内容
  • Section_A仅保留第一组输入输出:原有代码在每组BIT_MATCH/输入输出对读取完成后,直接覆盖了对应Section的输入输出列表,没有做追加操作,同时没有正确判断同Section下多组输入输出的边界

最小改动修复方案

保留原有逐行遍历、关键字匹配的核心逻辑,仅做3处调整即可解决问题:

  1. 新增start_parse标记,首次遇到Section行后设为True,后续所有行都进入解析逻辑,不再跳过
  2. 每个Section下新增input_list、output_list数组,每次读取到input->/output->值时追加到对应数组,而不是直接赋值
  3. 遇到新的Section行时,先将上一个Section的数据存入最终结果列表,再初始化新的Section参数

修复后核心代码示例

def parse_machinary_file(file_path):
    result = []
    start_parse = False
    current_section = None
    current_inputs = []
    current_outputs = []
    current_bit_match = "N/A"
    current_input_obtained = "N/A"
    current_output_obtained = "N/A"

    with open(file_path, 'r', encoding='utf-8') as f:
        for line in f:
            line = line.strip()
            # 跳过首个Section之前的所有内容
            if not start_parse:
                if "Section" in line:
                    start_parse = True
                else:
                    continue
            # 处理新Section行
            if "Section" in line:
                # 先保存上一个Section的已解析数据
                if current_section is not None:
                    result.append({
                        "section_name": current_section,
                        "BIT_MATCH": current_bit_match,
                        "Input Obtained": current_input_obtained,
                        "Output Obtained": current_output_obtained,
                        "input_list": current_inputs if current_inputs else ["N/A"],
                        "output_list": current_outputs if current_outputs else ["N/A"]
                    })
                # 初始化新Section的参数
                current_section = line
                current_inputs = []
                current_outputs = []
                current_bit_match = "N/A"
                current_input_obtained = "N/A"
                current_output_obtained = "N/A"
                continue
            # 匹配关键字行做对应处理
            if line.startswith("BIT_MATCH"):
                current_bit_match = line.split("=", 1)[-1].strip() if "=" in line else "N/A"
            elif line.startswith("Input Obtained"):
                current_input_obtained = line.split("=", 1)[-1].strip() if "=" in line else "N/A"
            elif line.startswith("Output Obtained"):
                current_output_obtained = line.split("=", 1)[-1].strip() if "=" in line else "N/A"
            elif line.startswith("input->"):
                input_val = line.split("->", 1)[-1].strip()
                current_inputs.append(input_val if input_val else "N/A")
            elif line.startswith("output->"):
                output_val = line.split("->", 1)[-1].strip()
                current_outputs.append(output_val if output_val else "N/A")
        # 遍历结束后保存最后一个Section的数据
        if current_section is not None:
            result.append({
                "section_name": current_section,
                "BIT_MATCH": current_bit_match,
                "Input Obtained": current_input_obtained,
                "Output Obtained": current_output_obtained,
                "input_list": current_inputs if current_inputs else ["N/A"],
                "output_list": current_outputs if current_outputs else ["N/A"]
            })
    return result

改动验证

  • 所有空值统一返回N/A,符合需求要求
  • Section_A的多组输入输出会全部追加到对应列表,不会出现仅保留第一组的问题
  • 首个Section之后的所有Section都会被正常解析,不会丢失Section_B等后续条目

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 19:54:07