The core regular modules were taken directly from the Apachelog module, and I almost didn't do anything.
Apahelog Module Address: http://pypi.python.org/pypi/apachelog/1.0 #!/usr/bin/env python #coding =gbk "" "Apache Log Parser, Module Apahcelog: http://pypi.python.org/pypi/apachelog/1.0 "" "Import re Import sys Import configparser Def getformat (): # get ' format ' from Format.ini config = Config Parser.configparser () Try: INIFH = open (' Format.ini ') except IOError: print "Can not load Format.ini!" Sys.exit (1) CONFIG.READFP (INIFH) FormatName = " format = {} try: &NBSP ; FormatName = config.get (' type ', ' name ') Except configparser.nooptionerror: print "Bad section/option name! Please check Format.ini " Sys.exit (1) Try: OPtions = config.options (' format ') Except configparser.nooptionerror: Print ' Can not enum option [format] ' Sys.exit (1) for opt in options: if opt not in Format.keys (): format[opt] = Config.get (' format ', opt) ELSE: print "duplicate name in O ption [format] " Sys.exit (1) Try: RET = Format[formatname] Except keyerror: print "Format/"%s/"has not been Defined in Format.ini "%formatname Sys.exit (1) Return ret # intercept from Module Apachelog class apachelogparsererror (Exception): Pass Class parser (): & nbsp DEF __INIT__ (Self, format): self._names = [] Self._regex = None & nbsp Self._pattern = " Self._parse_format (format) Def _pars E_format (self, format): format = Format.strip () format = re.sub (' [ /t]+ ', ' ', format} subpatterns = [] &NBS P Findquotes = Re.compile (R ' ^//"') findreferreragent = Re.compile (' referer| User-agent ') findpercent = re.compile (' ^%.*t$ ') lstripquotes = Re. Compile (R ' ^//') rstripquotes = Re.compile (R '//"$ ') for element in Format.split ('): Hasquotes = 0 If findquotes.search (element): Hasquotes = 1 &N Bsp If hasquotes: element = Lstripquotes.sub (", Element) & nbsp element = Rstripquotes.sub (", Element) self._names.append (Self.alias (Element)) &NBS P Subpattern = ' (/s*) ' &NB Sp if hasquotes: If element = = '%r ' or findreferreragent.search (element): Subpatter n = R '/' ([^]//]* (?:/ /. [^ "//]*" *)/"' ELSE: &NBsp Subpattern = R '/"([^/"]*)/"' &N Bsp Elif findpercent.search (Element): Subpattern = R ' (/[[^/]]+/]) ' & nbsp Elif element = = '%u ': Subpattern = ' (. +?) ' &nbs P Subpatterns.append (subpattern) Self._pattern = ' ^ ' + ' '. Join (Subpatterns) + ' $ ' Try: Self._regex = Re.compile (self._pattern) EXCEPT&NBSP ; Exception, E: Raise apachelogparsererror (e) &NB Sp Def parse (self, line): line = Line.strip () match = sel F._regex.match (line) If match: &N Bsp data = {} for k, V in Zip (self._names, match.groups