`
mingren135
  • 浏览: 69082 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Python实战1-解析日志模块

 
阅读更多

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

 

该模块作用有2个:

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

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

 

 

logparse.py

def 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

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics