withopen(file_path, 'r') as file: for line in file: # 处理端口信息行 if line.startswith('["port'): port_info.append(eval(line)) # 处理传输信息行 elif line.startswith('('): transmission_info.append(eval(line)) # 处理数据行 elif line.strip() andnot line.startswith(('(', '[')): data_lines.append(line.split()) data_len = [] for i inrange(int(len(transmission_info)/2)): data_len.append(transmission_info[2*i+1]) transmission_info = [j for j in transmission_info if j notin data_len] data_len = [t[0] for t in data_len]
# 提取数据行中的数值数据 numerical_data = [] for line in data_lines: row_data = [] for element in line: try: # 尝试将元素转换为浮点数 row_data.append(float(element)) except ValueError: # 如果转换失败,忽略该元素 continue if row_data: numerical_data.append(row_data)
# 按照传输信息分类数值数据 classified_data = {} for i inrange(len(transmission_info)): key = tuple(transmission_info[i]) if key notin classified_data: classified_data[key] = [] classified_data[key] = numerical_data[sum(data_len[:i]):sum(data_len[:i+1])]