觉得运维同事徒眼分析日志数据太麻烦,刚好工作也忙完了,于是乎用Python写了个小程序帮同事减轻下负担~
功能:把重要的数据提取出来,画成Excel文件~
说明:Python大佬们对于程序中觉得有可优化与不合理之处,欢迎指教~
代码如下:
1 import xlwt 2 import xlrd 3 import io 4 import pandas 5 import numpy 6 7 fileaddresstlog='F:abc.log' 8 fileaddress='d:日志分析_abc.xls' 9 10 def read_log(): 11 f = xlwt.Workbook(encoding = 'utf-8') # 实例化一个工作簿对象 12 sheet1 = f.add_sheet('今天学习了吗') # 获取工作表对象Worksheet 13 log=open(fileaddresstlog,'r', encoding = 'utf-8') 14 lines = log.readlines() 15 r=0 16 content = ['序号','用户', '事件', '时间', '游戏ID', '游戏名称', '用户停留时长', 'IP'] 17 write_excel(r,content,sheet1) # 向工作表中添加数据(参数对应 行, 列, 值) 18 r=r+1 19 for logline in lines: 20 if 'tjfp' in logline: 21 usrKey = 'uid=' 22 timeKey = '&triggerTime=' 23 eventKey = '&eventName=' 24 appIdKey = '&appId=' 25 appNameKey = '&appName=' 26 ipKey = '&terminalIP=' 27 usrStartIndex = logline.find(usrKey) + len(usrKey) 28 usrEndIndex = logline.find(timeKey) 29 eventStartIndex = logline.find(eventKey) + len(eventKey) 30 eventEndIndex = logline.find(appIdKey) 31 timeStartIndex = logline.find(timeKey) + len(timeKey) 32 timeEndIndex = logline.find(eventKey) 33 appIdStartIndex = logline.find(appIdKey) + len(appIdKey) 34 appIdEndIndex = logline.find(appNameKey) 35 appNameStartIndex = logline.find(appNameKey) + len(appNameKey) 36 ipStartIndex = logline.find(ipKey) + len(ipKey) 37 ipEndIndex = len(logline) 38 durVal = '' 39 if 'duration' in logline: 40 durKey = '&duration=' 41 durStartIndex = logline.find(durKey) + len(durKey) 42 durEndIndex = logline.find(ipKey) 43 durVal = logline[durStartIndex:durEndIndex] 44 appNameEndIndex = logline.find(durKey) 45 else: 46 appNameEndIndex = logline.find(ipKey) 47 content = [r,logline[usrStartIndex:usrEndIndex], logline[eventStartIndex:eventEndIndex], logline[timeStartIndex:timeEndIndex], logline[appIdStartIndex:appIdEndIndex], logline[appNameStartIndex:appNameEndIndex], durVal, logline[ipStartIndex:ipEndIndex]] 48 write_excel(r,content,sheet1) # 向工作表中添加数据(参数对应 行, 列, 值) 49 r=r+1 50 f.save(fileaddress) # 保存数据到硬盘 51 52 53 def write_excel(r,row,sheet1): 54 print(r) 55 font = xlwt.Font() # 为样式创建字体 56 font.name = 'name Times New Roman' # 字体类型 57 font.height = 20*11 # 字体大小,11为字号,20为衡量单位 58 alignment = xlwt.Alignment() # 设置单元格对齐方式 59 alignment.horz = 0x02 # 0x01(左端对齐)、0x02(水平方向上居中对齐)、0x03(右端对齐) 60 alignment.vert = 0x01 # 0x00(上端对齐)、 0x01(垂直方向上居中对齐)、0x02(底端对齐) 61 alignment.wrap = 1 #设置自动换行 62 borders = xlwt.Borders() # 设置边框 63 # 细实线:1,小粗实线:2,细虚线:3,中细虚线:4,大粗实线:5,双线:6,细点虚线:7 64 # 大粗虚线:8,细点划线:9,粗点划线:10,细双点划线:11,粗双点划线:12,斜点划线:13 65 borders.left = 1 66 borders.right = 1 67 borders.top = 1 68 borders.bottom = 1 69 # 初始化样式 70 style = xlwt.XFStyle() 71 style.font = font 72 style.alignment = alignment 73 style.borders = borders 74 for i in range(0,len(row)): 75 # 设置列宽,一个中文等于两个英文等于两个字符,11为字符数,256为衡量单位 76 if i == 0: 77 sheet1.col(i).width = 11 * 256 78 else: 79 sheet1.col(i).width = 30 * 256 80 sheet1.write(r,i,row[i], style) 81 82 83 if __name__=='__main__': 84 read_log() 85 print('end')
微信公众号:敲代码的小浪漫
欢迎大家关注~