读取log文件,分析数据,写入txt文件

 1 from xlwt import *
 2 import os
 3 import re
 4 #------------------读数据---------------------------------
 5 #判断是否存在有此类型的文件且文件名一致
 6 #--log文件--
 7 filename_bdc01_log = "ubp_bdc01.log"
 8 #---文件名字列表---
 9 filenames = []#创建空列表的名字集合
10 filenames.append(filename_bdc01_log)
11 
12 data_list = []#error数据列表
13 
14 for name in filenames: 
15   filePath = "D:广东应急厅巡检日志"
16   os.chdir(filePath)
17   path = os.getcwd()
18   if os.path.exists(name):
19     file_only_open = open(name, "r")
20     if name == filename_bdc01_log:
21           lines = file_only_open.readlines()
22           #data_list.append(name.strip(".") + ':' + '
')
23           for line in lines:
24               matchObj = re.search( r'RTPIdleTimeThd reached', line,re.I)
25               if matchObj:
26                  data_list.append(line)
27           data_list.append('
')
28 
29           for i in range(0,len(lines)):
30               matchObj = re.search( r'Exception', lines[i])
31               if matchObj:
32                  matchRes = re.search(r'Exception(.*)',lines[i])
33                  data_list.append(matchRes.group())
34                  data_list.append('
')
35           data_list.append('
')
36   else:
37     print("没有找到此文件:" + name)
38 
39 # 文件读写方式是追加
40 file_new = open("BDC巡检报告error数据汇总.txt", "w+")
41 for data in data_list:
42     file_new.writelines(str(data))
43 file_new.close()
原文地址:https://www.cnblogs.com/dog-and-cat/p/11507871.html