import re import os import xlwt ''' 1、读取数据 2、处理数据 3、写入数据 ''' path = "./5.log" f = open(path, encoding='utf8') lines = f.readlines() file_name = '提取数据.xls' if os.path.exists(file_name): os.remove(file_name) # 写入数据 num = 1 # 创建工作簿 file = xlwt.Workbook() sheet1 = file.add_sheet("Sheet1", cell_overwrite_ok=True) sum = 0 sum2 = 0 index_dict = [] for line in lines: if "接口总耗时" in line: result = re.findall(".*调用(.*)接口总耗时.*", string=line) data = re.findall(".*接口总耗时:(.*)ms.*", string=line) ''' 1、创建一个数组, 2、校验当前的key,符合存入数组 3、从数组中取出,写入Excel ''' if result[0] == "processBarcode" and sum < 5: index_dict.append(data) sum = sum + 1 elif result[0] == "changeQuantity" and sum2 < 2: index_dict.append(data) sum2 = sum2 + 1 elif result[0] == "lineVoid": index_dict.append(data) elif result[0] == "processItems": index_dict.append(data) elif result[0] == "total": index_dict.append(data) elif result[0] == "TPCS payment": index_dict.append(data) elif result[0] == "endTa": index_dict.append(data) sum = 0 sum2 = 0 j = 0 i = 0 k = 0 for item in range(len(index_dict)): if i < 12 * (k + 1): sheet1.write(j, k, index_dict[item]) j = j + 1 if j == 12: j = 0 k = k + 1 i = i + 1 file.save(file_name)