python统计log日志的数据

 

# -*- coding: cp936 -*-
#Filename:count_time
import re

fopen=open('c:\ex120821.log','r')

pattStatus=' - 200 '
#根据需求内容的不同选择不同的正则表达式
pattDate='(\d+-\d+-\d+)' #加括号表示返回括号的内容,

pattHour=' (\d+):'

adict={ }

def inputRead(data):
    for line in fopen:
        if re.findall(pattStatus, line)!=[]:
             
             
             ##获得时段--次数字典
             m=re.findall(pattHour, line)
             if m!=[]:
                 key=m[0]
                 if adict.has_key(key):
                     adict[key]=adict[key]+1
                 else:
                     adict[key]=1

    fwrite=open('c:\\'+date+'.txt', 'w')
    fwrite.write('时段')
    fwrite.write('     ')
    fwrite.write('次数\n')  
    for key in adict.keys():
        fwrite.write(' '+key)
        fwrite.write('       ')
        fwrite.write((str)(adict[key])+'\n')
    fwrite.close()
    fopen.close()
     
for line in fopen:
    date=re.findall(pattDate, line)
    obj=''
    for item in date:
        if item!=obj:
            print item
            print type(item)
            print type(obj)
            inputRead(item)
        obj=item

你可能感兴趣的:(python)