1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
import os import re import xlsxwriter as xw from xlsxwriter import Workbook
filename = 'F:/SR66_Test/xxx.log' resultfilename = 'F:/SR66_Test/result.txt' if os.path.exists(resultfilename): os.remove(resultfilename) result_txt = open('F:/SR66_Test/result.txt', 'a')
with open(filename, 'r', encoding='utf8') as file_to_read: while True: lines = file_to_read.readline() if "GE" in lines: result_line1 = re.sub(r'^.*?#_', '', lines) result_line1 = result_line1.replace("\n", "\t") print(result_line1) result_txt.write(result_line1) if "--- Ping statistics" in lines: result_line2 = re.sub(r'^.*?for', 'ping', lines) result_line2 = result_line2.replace("-", "") result_line2 = result_line2.replace("\n", "\t") print(result_line2) result_txt.write(result_line2) lines = next(file_to_read) result_line3 = re.sub(r'^.*?received,', 'result:', lines) print(result_line3) result_txt.write(result_line3) result_txt.flush() if "结束记录时间" in lines: break
workbook: Workbook = xw.Workbook('F:/SR66_Test/result.xlsx')
sheet0 = workbook.add_worksheet('测试结果') centered = workbook.add_format({'align': 'center'}) red_style = workbook.add_format({ "fg_color": "red",'align': 'center','bold': True}) green_style = workbook.add_format({ "fg_color": "green",'align': 'center','bold': True}) blue_style = workbook.add_format({ "fg_color": "#8DB4E2",'align': 'center'}) sheet0.write(0, 0, '检测端口', blue_style) sheet0.write(0, 1, '对端IP地址', blue_style) sheet0.write(0, 2, '返回结果', blue_style) sheet0.write(0, 3, '检测结果', blue_style) sheet0.write(0, 4, '线路正常', green_style)
sheet0.write(0, 6, '检测失败', red_style)
sheet0.set_column(0, 0, width=20) sheet0.set_column(1, 1, width=24) sheet0.set_column(2, 2, width=28) sheet0.set_column(3, 3, width=12.38) sheet0.set_column(4, 4, width=12.38) sheet0.set_column(5, 5, width=5) sheet0.set_column(6, 6, width=12.38) sheet0.set_column(7, 7, width=5) sheet0.freeze_panes(1, 8, top_row=1)
sheet0.filter_column_list(3, 'x = "正常"') sheet0.write_formula('D2', '{=IF(ISNUMBER(FIND("100.0%",C2)),"检测失败",IF(ISNUMBER(FIND("0.0%",C2)),"正常"))}') sheet0.write_formula('F1', '{=SUMPRODUCT((D2:D999="正常")*1)}') sheet0.write_formula('H1', '{=SUMPRODUCT((D2:D999="检测失败")*1)}')
with open('D:/SR6608_Test/result.txt') as file_object: lines = file_object.readlines() file_object.close()
i = 1 x = 1 for xls_line in lines: if "GE" in xls_line: xls_line = xls_line.strip('\n') xls_line = xls_line.split('\t') print(xls_line) port_info = xls_line[0] ip_addr = xls_line[1] return_info = xls_line[2] sheet0.write(i, 0, port_info, ) sheet0.write(i, 1, ip_addr, ) sheet0.write(i, 2, return_info, ) i += 1 x += 1
workbook.close()
|