Parsing apache log files

前端 未结 6 939
你的背包
你的背包 2020-12-01 01:39

I just started learning Python and would like to read an Apache log file and put parts of each line into different lists.

line from the file

1

6条回答
  •  不知归路
    2020-12-01 02:19

    This is a job for regular expressions.

    For example:

    line = '172.16.0.3 - - [25/Sep/2002:14:04:19 +0200] "GET / HTTP/1.1" 401 - "" "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.1) Gecko/20020827"'
    regex = '([(\d\.)]+) - - \[(.*?)\] "(.*?)" (\d+) - "(.*?)" "(.*?)"'
    
    import re
    print re.match(regex, line).groups()
    

    The output would be a tuple with 6 pieces of information from the line (specifically, the groups within parentheses in that pattern):

    ('172.16.0.3', '25/Sep/2002:14:04:19 +0200', 'GET / HTTP/1.1', '401', '', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.1) Gecko/20020827')
    

提交回复
热议问题