如何用Python在in.txt中查找指定number值并验证info字段条件?
问题描述
我有一个文件in.txt,内容如下:
name="XYZ_PP_0" number="0x12" bytesize="4" info="0x0000001A" name="GK_LMP_2_0" number="0xA5" bytesize="8" info="0x00000000bbae321f" name="MP_LKO_1_0" number="0x356" bytesize="4" info="0x00000234" name="PNP_VXU_1_2_0" number="0x48A" bytesize="8" info="0x00000000a18c3ba3" name="AVU_W_2_3_1" number="0x867" bytesize="1" info="0x0b"
需要完成两个任务:
- 查找
number="0x867"的行,验证其info值是否等于预期的0x0a,匹配则输出matches,否则输出doesn't matches; - 查找
number="0x12"的行并存储其info值,查找number="0x356"的行存储其info值,验证后者是否等于前者加0x00000004(即0x0000001A + 0x00000004 = 0x0000001E),匹配则输出number="0x12" info value 0x0000001A + 0x00000004 matches to info value of number="0x356".,否则输出resulted value not matching。
我当前的Python尝试代码如下:
with open("in.txt", "r") as infile: XYZ = False MP = False AVU = False xyz = ['number="0x12"', 'info="0x0000001A"'] mp = ['number="0x356"', 'info="0x00000234"'] avu = ['number="0x867"', 'info="0x0b"'] for line in infile: if all(x in line for x in xyz): XYZ = True continue if all(x in line for x in mp): MP = True continue if all(x in line for x in avu): AVU = True continue
但该代码仅能检查对应行是否存在,无法实现上述条件验证。请问如何实现查找指定number值并存储对应info值以完成所需条件验证?
解决方案
核心思路是解析每行的键值对,提取number和info的数值后再做验证,具体实现步骤如下:
1. 编写行解析函数
把每行的key="value"格式转换成字典,方便快速提取目标字段:
def parse_line(line): parts = line.strip().split() data = {} for part in parts: key, value = part.split('=', 1) data[key] = value.strip('"') return data
2. 遍历文件收集目标数据
初始化变量存储需要的info值,遍历文件时匹配指定number并记录对应info的十进制数值(方便后续计算):
info_0x12 = None info_0x356 = None info_0x867 = None with open("in.txt", "r") as infile: for line in infile: line = line.strip() if not line: continue # 跳过空行 data = parse_line(line) if data['number'] == '0x12': info_0x12 = int(data['info'], 16) elif data['number'] == '0x356': info_0x356 = int(data['info'], 16) elif data['number'] == '0x867': info_0x867 = int(data['info'], 16)
3. 执行两个验证任务
任务1:验证0x867的info值
expected_0x867 = 0x0a print("matches" if info_0x867 == expected_0x867 else "doesn't matches")
任务2:验证0x356的info是否等于0x12的info加0x4
if info_0x12 is not None and info_0x356 is not None: expected_0x356 = info_0x12 + 0x4 if info_0x356 == expected_0x356: # 把十进制转回8位十六进制字符串,保持和原文件格式一致 print(f'number="0x12" info value 0x{info_0x12:08X} + 0x00000004 matches to info value of number="0x356".') else: print("resulted value not matching") else: print("Missing required lines (0x12 or 0x356)")
完整代码
整合所有部分后的完整代码:
def parse_line(line): parts = line.strip().split() data = {} for part in parts: key, value = part.split('=', 1) data[key] = value.strip('"') return data info_0x12 = None info_0x356 = None info_0x867 = None with open("in.txt", "r") as infile: for line in infile: line = line.strip() if not line: continue data = parse_line(line) if data['number'] == '0x12': info_0x12 = int(data['info'], 16) elif data['number'] == '0x356': info_0x356 = int(data['info'], 16) elif data['number'] == '0x867': info_0x867 = int(data['info'], 16) # 执行任务1 expected_0x867 = 0x0a print("matches" if info_0x867 == expected_0x867 else "doesn't matches") # 执行任务2 if info_0x12 is not None and info_0x356 is not None: expected_0x356 = info_0x12 + 0x4 if info_0x356 == expected_0x356: print(f'number="0x12" info value 0x{info_0x12:08X} + 0x00000004 matches to info value of number="0x356".') else: print("resulted value not matching") else: print("Missing required lines (0x12 or 0x356)")
代码说明
parse_line函数将每行字符串转为字典,简化字段提取操作;- 把info值转为十进制整数,是为了方便进行加法运算;
- 输出时用
0x{info_0x12:08X}将整数转回8位大写十六进制字符串,和原文件格式保持一致; - 增加了缺失行判断,避免出现
None值运算报错。
内容的提问来源于stack exchange,提问作者V_S
相关产品推荐
相关产品推荐

