读书人

Python实战1-解析日记模块

发布时间: 2012-12-27 10:17:10 作者: rapoo

Python实战1-解析日志模块

根据用户提供的分隔符,分解日志数据

?

该模块作用有2个:

1)提前检测能否正确解析日志,否则用户需要修改分隔符,或者日志格式

2)准备导入数据库的数据

?

?

logparse.pydef parselogline(line, tupleseperatorlist):    '''    input        line: line in log file        tupleseperatorlist: seperators in tuple    output        splited values    '''    data = []        for tupleseperator in tupleseperatorlist:        head = tupleseperator[0]        end = tupleseperator[1]                if not head :             endindex = line.find(end)            value = line[0:endindex]        elif not end:            headindex = line.find(head)            value = line[headindex + len(head) :]        else :            headindex = line.find(head)            endindex = line.find(end)            value = line[headindex + len(head): endindex]        data.append(value)     return data

?

读书人网 >perl python

热点推荐