国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python實現掃描日志關鍵字的示例

2020-02-22 23:55:25
字體:
來源:轉載
供稿:網友

我們在壓力測試過程會收集到很多log,怎樣快速從中找到有用信息呢?讓python腳本幫我們做這部分工作吧!

廢話不說,上代碼

環境:win10 + python2.7.14

#-*- encoding: utf-8 -*-#author : beihuijie#version 1.1import reimport sysimport osimport countTimedef getParameters(): ''' get parameters from console command ''' with open(sys.argv[1], "r") as fread: lines = fread.readlines() keywords=[] for line in lines:  temp = line.split(', ')  keywords.append(temp) for i in range(0, (len(keywords[0]) - 1)):  print ' Keyword = %s' % keywords[0][i] return keywords[0]def isFileExists(strfile): ''' check the file whether exists ''' return os.path.isfile(strfile)def Search(keyword, filename): ''' search the keyword in a assign file ''' if(isFileExists(filename) == False): print 'Input filepath is wrong,please check again!' sys.exit() linenum = 1 findtime = 0 with open(filename, 'r') as fread: lines = fread.readlines() for line in lines:  rs = re.findall(keyword, line, re.IGNORECASE)  if rs:  #output linenum of keyword place   sys.stdout.write('line:%d '%linenum)  lsstr = line.split(keyword)  strlength = len(lsstr)  findtime = findtime + 1  #print strlength  for i in range(strlength):   if(i < (strlength - 1)):   sys.stdout.write(lsstr[i].strip())   sys.stdout.write(keyword)   else:   sys.stdout.write(lsstr[i].strip() + '/n')  linenum = linenum + 1 print '+----------------------------------------------------------------------------+' print (' Search result: find keyword: %s %d times'%(keyword, findtime)) print '+----------------------------------------------------------------------------+'def executeSearch(): ''' this is a execute search method ''' ls = getParameters() start = countTime.getTime() parameter_number = len(ls) print 'Filename = %s ' % ls[parameter_number - 1] print '--------------------start search-------------------------' if(parameter_number >= 2): for i in range(parameter_number - 1):  Search(ls[i], ls[parameter_number - 1]) else: print 'There is a parameter error occured in executeSearch()!' end = countTime.getTime() print '+----------------------------------------------------------------------------+' print ' Total cost time: %s'%countTime.formatTime(end - start) print '+============================================================================+' if __name__=='__main__': executeSearch()

countTime.py

#-*- encoding: utf-8 -*-#author : beihuijie#version 1.1import datetimeimport timedef getTime(): ''' return time is format of time(unit is second) ''' return time.time()def getCPUClockTime(): ''' return time is CPU Clock time ''' return time.clock()def formatTime(timevalue): ''' format the time numbers ''' hour = 0 minute = 0 second = 0 if timevalue > 0: #count hour hour = timevalue // 3600 remain = timevalue % 3600 #count minute minute = remain // 60 remain = remain % 60 #count second second = round(remain, 3) return '%.0fh:%.0fm:%.3fs'%(hour, minute, second) if __name__=='__main__': value = 134.45632 print value print formatTime(value)             
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 台东县| 陇西县| 台北县| 吴旗县| 广灵县| 商都县| 台安县| 古交市| 汤阴县| 万载县| 枣庄市| 南通市| 南皮县| 吴旗县| 沂源县| 吴江市| 永兴县| 买车| 咸阳市| 上饶市| 桐庐县| 泾川县| 冕宁县| 东城区| 永丰县| 南澳县| 梁平县| 吉首市| 昆明市| 潮安县| 工布江达县| 平乐县| 平远县| 利津县| 西乡县| 惠来县| 射洪县| 新民市| 庆云县| 通江县| 玉溪市|