fp =open("e:\\data.log")
for line in fp.readlines(): # 遍历每一行
    filename = line[:14]    # 每行取前14个字母,作为下面新建文件的名称
    content = line[14:]     # 每行取第15个字符后的所有字符,作为新建文件的内容
    with open("e:\\"+filename+".txt","w") as fp2:
        fp2.write(content+"\n")
fp.close()
View Code

参考链接[1] .

2 提取目标信息

日志文件每行字符串由空格分隔,例如对第1个字段(IP、时间等)感兴趣,则使用split()方法对每行字符串进行切片,将第1个子字符串存到列表里,用于下一步处理。

示例代码:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
txt = "Google#Runoob#Taobao#Facebook"
# 第二个参数为 1,返回两个参数列表
x = txt.split("#", 1)
print x

输出结果:

['Google', 'Runoob#Taobao#Facebook']

参考链接[2] .

3 统计分析

在上一步骤中,将感兴趣的目标信息存储到列表中,现使用python统计列表元素出现的次数, 参考链接[3] 提供了很多实现方法 [4] ,本文使用collections [5] 中的most_common()方法。

from collections import Counter
def counter(arr):
    return Counter(arr).most_common(2) # 返回出现频率最高的两个数
# 结果:[(2, 3), (1, 2)]

参考链接[ 3 - 4 - 5 ]

完整代码(待整理):

IP_CountResult = Counter(IPlists).most_common() # print IP_CountResult # print '[0][0]', IP_CountResult[0][0] print ' Length of IP_CountResult: ' , len(IP_CountResult) f = xlwt.Workbook() # Create workbook sheet1 = f.add_sheet(u ' sheet1 ' ,cell_overwrite_ok=True) # Create sheet row0 = [u ' IP ' , u ' Count ' ] # Create first row for i in range(0,len(row0)): sheet1.write(0, i, row0[i]) for i in range(0,len(IP_CountResult)): for j in range(0,len(IP_CountResult[i])): sheet1.write(i +1 , j, IP_CountResult[i][j]) f.save( ' d:\\IP_CountResult.xls ' ) # Save the file # ===================================== # 测试字符串切片(分割) # txt = "Google Runoob Taobao Facebook" # 第二个参数为 1,返回两个参数列表 # x = txt.split(" ", 1) # print x[0] # ===================================== # filename = line[:14] # content = line[14:] # with open("e:\\"+filename+".txt","w") as fp2: # fp2.write(content+"\n") View Code

其他拓展应用,见链接[6-9]

另,研究pandas在数据处理、绘图等方面的应用。

参考链接:

[1] python文件操作--分析系统log并提取有效数据 : https://blog.csdn.net/qq_30758629/article/details/80766583

[2] 菜鸟教程 - Python split()方法 : http://www.runoob.com/python/att-string-split.html

[3] Python统计列表元素出现次数 : https://blog.csdn.net/weixin_40604987/article/details/79292493

[4] get_frequency : https://github.com/KARL13YAN/learning/blob/master/get_frequency.py

[5] collections官方文档 : https://docs.python.org/3/library/collections.html

[6] python读取日志 - 周一到周五早上6点半检查日志中的关键字,并发送相应的成功或者失败短信 : https://blog.csdn.net/shirayukixue/article/details/52120110

日志内容如下:

[16-08-04 06:30:39] Init Outer: StkID:20205 Label:7110 Name:02ͨ Type:3 PriceDigit:4 VolUnit:0 FloatIssued:                                                               0 TotalIssued:                                                               0 LastClose:0 AdvStop:0 DecStop:0
[16-08-04 06:30:39] Init Outer: StkID:20206 Label:9802 Name:982 Type:3 PriceDigit:4 VolUnit:0 FloatIssued:                                                               0 TotalIssued:                                                               0 LastClose:0 AdvStop:0 DecStop:0
[16-08-04 06:30:39] IB Recv DCT_STKSTATIC, Stock Total = 20207 Day=20160804, Ver=1470283608
View Code

配置文件如下:

import thread from logging.handlers import RotatingFileHandler from apscheduler.schedulers.blocking import BlockingScheduler # 通过logging.basicConfig函数对日志的输出格式及方式做相关配置 logging.basicConfig(level= logging.DEBUG, format = ' %(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s ' , datefmt = ' %a, %d %b %Y %H:%M:%S ' , filename = ' search.log ' , filemode = ' a ' ) #定义一个StreamHandler,将INFO级别或更高的日志信息打印到标准错误,并将其添加到当前的日志处理对象 console = logging.StreamHandler() console.setLevel(logging.INFO) formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s') console.setFormatter(formatter) logging.getLogger('').addHandler(console) #定义一个RotatingFileHandler,最多备份5个日志文件,每个日志文件最大10M Rthandler = RotatingFileHandler('search.log', maxBytes=1*1024*1024,backupCount=2) Rthandler.setLevel(logging.INFO) #formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s') #Rthandler.setFormatter(formatter) logging.getLogger('').addHandler(Rthandler) # 读取配置文件 try : conf = ConfigParser.ConfigParser() # 生成config对象 conf.read( " search.ini " ) # 用config对象读取配置文件 # keys = conf.get("main","keys") #指定session,option读取值 # logpath = conf.get("main","filepath") mobileno = conf.items( " MobileNo " ) # failmsg = conf.get("msg","fail") code = conf.sections() except Exception as exc: def getconfig(section): # 定义全局变量 global keys, logpath, exepath, exefile, failmsg, day_of_week, hour ,minute keys = conf.get(section, " keys " ) logpath = conf.get(section, " filepath " ) exepath = conf.get(section, " exepath " ) exefile = conf.get(section, " exefile " ) failmsg = conf.get(section, " failmsg " ) day_of_week = conf.get(section, " day_of_week " ) hour = conf.get(section, " hour " ) minute = conf.get(section, " minute " ) print keys, logpath, exepath, exefile, failmsg, day_of_week, hour ,minute if len(sys.argv) != 3: print 'Please input correct parameter,for example:' print 'python search.py keyword filepath configpath' else: for i in range(1,len(sys.argv)): print i, sys.argv[i] ret.append(sys.argv[i]) print '+============================================================================+' print ' Keyword = %s'%sys.argv[1] return ret def isFileExists(strfile): # 检查文件是否存在 return os.path.isfile(strfile) def sendMailOk(timefile): # 初始化正常,发送邮件 datetimes = timefile.split( ' [ ' ) times = datetimes[1].split( ' ] ' ) code = timefile.split() init = [times[0],code[2], " init success! " ] message = ' ' .join(init) # 使用字符串的join方法,可以把列表的各个元素连接起来 logging.info(message) url = ' http://***/smsNew/sendMessage.html ' # payload = {'clientId':'804D0196-6C0D-4CEF-91E1-1BB85E0217DB','Code':'GB2312','toMobileNo':toMobileNo,'message':message} # r = requests.post('http://***/smsNew/sendMessage.html?clientId=804D0196-6C0D-4CEF-91E1-1BB85E0217DB&Code=GB2312&toMobileNo=18516235206&message=test') # r = requests.post(url,params = payload) # print r.url,r.text for i in range(len(mobileno)): toMobileNo = mobileno[i][1 ] payload = { ' clientId ' : ' 804D0196-6C0D-4CEF-91E1-1BB85E0217DB ' , ' Code ' : ' GB2312 ' , ' toMobileNo ' :toMobileNo, ' message ' :message} r = requests.post(url,params = payload) print r.url,r.text print toMobileNo # print r.text # getConfig() def sendMalFail(): # 初始化失败发送短信 url = ' http://***/smsNew/sendMessage.html ' for i in range(len(mobileno)): toMobileNo = mobileno[i][1 ] payload = { ' clientId ' : ' 804D0196-6C0D-4CEF-91E1-1BB85E0217DB ' , ' Code ' : ' GB2312 ' , ' toMobileNo ' :toMobileNo, ' message ' :failmsg} r = requests.post(url,params = payload) logging.error(failmsg) print r.url,r.text print toMobileNo def Search(keyword, filename): # 在文件中搜索关键字 if (isFileExists(filename) == False ): # print 'Input filepath is wrong,please check agin!' logging.error( ' Input filepath is wrong,please check agin! ' ) return False # sys.exit() linenum = 1 with open(filename, ' r ' ) as fread: lines = fread.readlines() for line in lines: rs = re.search(keyword, line) if rs: # 打印关键字所在行 # sys.stdout.write('line:%d '%linenum) # print line lsstr = line.split(keyword) # strlength = len(lsstr) # logging.info('DC init success! ') sendMailOk(lsstr[0]) #打印搜索关键字所在行信息 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') # 关闭打印日志程 killdc = " pkill " + exefile os.system(killdc) return True # sys.exit() linenum = linenum + 1 logging.debug( ' DC not init ,tye agin! ' ) return False def executeSearch(): ls = getParameters() if(len(ls) == 2): while True: for i in range(5): Search(ls[0], ls[1]) #初始化成功退出脚本,否则继续循环 #print i time.sleep(60) sendMalFail() #连续5次查找都没有初始化,发送失败短信 else: print 'There is a parameter error occured in executeSearch()!' # print keys,logpath,mobileno # os.system('cd /home/level2/ib && /bin/echo > datacollect.log && nohup ./dcib.exe > /dev/null 2>&1 &') startdc = " cd " + exepath + " && /bin/echo > datacollect.log && nohup ./ " + exefile + " > /dev/null 2>&1 & " os.system(startdc) time.sleep( 3 ) for i in range(5 ): if Search(keys,logpath)== True: return True time.sleep( 60 ) sendMalFail() while Search(keys,logpath) == False: time.sleep( 60 ) def cron(): scheduler = BlockingScheduler() scheduler.add_job(executeSearch, ' cron ' , day_of_week=day_of_week,hour=hour, minute= minute) scheduler.start() def main(): # scheduler = BlockingScheduler() for i in range(0,len(code)): if re.search( ' code ' ,code[i]): getconfig(code[i]) print " keys= " ,keys, " ; logpath= " ,logpath, " ; exepath= " ,exepath, " ; exefile= " ,exefile, " ; failmsg= " ,failmsg, " ; day_of_week= " ,day_of_week, " ; hour= " ,hour , " ; minute= " ,minute scheduler = BlockingScheduler() scheduler.add_job(executeSearch, ' cron ' , day_of_week=day_of_week,hour=hour, minute= minute) scheduler.start() # thread.start_new_thread(cron,()) # time.sleep(3) # executeSearch() if __name__ == ' __main__ ' : main() # executeSearch() View Code

[7] python每日一练 - 读取log文件中的数据,并画图表 : https://www.cnblogs.com/langzou/p/5986245.html

日志内容大致如下:

python处理代码:

[8] 统计一个文件中每个单词出现的次数,列出出现频率最多的5个单词 : https://www.jb51.net/article/137735.htm

[9] Python:string.count()返回str在string里面出现的次数 : https://blog.csdn.net/chixujohnny/article/details/50259585

示例代码:

s = 'this is a new technology,and I want to learn this.'
print(s.count('this', 0, len(s)))
#输出为2
View Code

转载于:https://www.cnblogs.com/Jaguar/p/10688427.html

原文链接: http://www.cnblogs.com/Jaguar/p/10688427.html 一、path环境变量设定 首先,需要设置你的环境变量path,具体你找到julia.exe的路径,比如 C:\\Users\\songroom\\AppData\\Local\\Julia-1.3.1\\bin\\julia.exe 那么,添加到path中: C:\\Users\\songroom\\AppData\\Local\\Julia-1.3.1\\bin\\ 二、julia与cmd的... 利用 python 语言实现 读取 log 文件 。并分析 文件 ,计算平均每个事务所用的平均时间,最长,最短时间,统计不完整事务(就是有开始没结束的情况)的总数和详情 部分源码: # -*- coding: cp936 -*- #操作系统:winxp # python 版本:2.5 #匹配事务并计算 from datetime import datetime from DTime import... 问题描述:需要 读取 log 文件 ,并获取出两天的 日志 做对比,查询增加或者减少量,规则化输出完成心得: python 的字典,很好用,读写时间快,用法自由#!/usr/bin/env python # -*- coding: utf-8 -*-# @Time : 17/8/18 下午5:08# @Author : lee# @File : read.py# @Software: PyCharm#... 在上一篇编写如何连接H2数据库查询数据的时候,出现了好些关于H2数据库的问题。现在一一提供我的解决方案。 1.创建H2数据库时,提示:org.h2.jdbc.JdbcSQLException:Afilepaththatisimplicitlyrelativetothecurrentworkingdirectoryisnotallowedinthedatabas... 1 # 文件 打开方式, 文件 打开是有返回值的2 #open函数3 #file_object = open(file_name,access_mode='r')4 #file_name 必填形参, access_mode='r' 变量 =值,缺省参数,传的话以传的为准,不传有默认值56 #file_object -简写——fo, 文件 对象7 #filedir = "D:/test.txt" #绝对路径... 通常在前期调试代码的时候,我们会使用print在IDE控制台打印一些信息,判断运行情况。但在运行整个自动化测试项目的过程中,通过print打印信息的方式获取运行情况显然行不通。 这时就需要收集 日志 ,每次运行后通过查看 日志 来获取项目运行情况。那么我们该如何获取 日志 ? 一, 日志 概述 1, 日志 作用 在项目开发或测试过程中,项目运行一旦出现问题,记录 日志 信息就显得尤为重要。主要通过 日志 来定位问题,就好比侦探人员要根据现场留下的线索来推断案情。 2, 日志 级别 代码在运行的过程中会出现不同的情况,如调试信息、警告信息 问题一:TypeError: 'str' object is not callable产生原因:该错误TypeError: 'str' object is not callable字面上意思:就是str不可以被系统调用,其实原因就是:你正在调用一个不能被调用的变量或对象,具体表现就是你调用函数、变量的方式错误.例子:filePath=kwargs['path']filePathStr=str(fi... 在项目中遇到这个问题,想把本地的 log 文件 通过 Server-Send Event 的形式 发送给前端。但是如何把那些 新增加 log 文本 读取 出来就成了问题。想过遍历 log 文件 取得行数, 读取 最后几行 文件 ,这种方式,不过缺点也很明显, log 文件 很有可能会重复。因此在网上看了些介绍,也查了些资料,整理了如下代码。start_point=0def read_ log s():fo = open("heak... 【十一月的萧邦】蛋蛋说我写的博客叫做情感博客,所以现在不忍心,出来水一篇,毕竟为了庆祝十一月的到来。 十一月有周杰伦的新专辑,当时我们买了藏在书包里,开运动会,一本漫画,一副耳机,躺在绿油油的草地上,哎,小姐,请问有没有卖半岛铁盒,走廊灯关上,恩恩听不清,我走到窗边书包放。。。。 感觉这些就是整个世界。这个其实叫做写在前面的话,因为现在是十一月的第一天,小时候超级喜欢十一月,因为每个运动会都会开 大体内容为: 读取 log 文件 ,从中获取loss值,accuracy值,并绘制曲线。 2. 曲线绘制 文件 读取 的形式可以参考上一篇文章,主体不变,主要部分还是在字符串分割上。 def getLossAcc( log File): f = open( log File, "r", encoding='utf-8') line = f.readline() # 以行的形式进行读