Basic questions regarding parsing logfiles

Ben S bens at replytothegroupplease.com
Wed May 14 20:04:18 EDT 2003


Hi,

Basically I'm trying to use Python to parse and process some log files
produced by a game. They take the fairly standard format of a time stamp
in the form of "Tue May 13 12:52:59 2003", and then some more
information. A few questions:

I load the file like this:

  logFile = file(filename, 'rU')
  lines = map(string.strip, logFile.readlines())

Is that a reasonably good way to do it (while stripping excess
whitespace too)?


Secondly, how do I parse that timestamp into one of the Python time
objects? I saw some ways which looked quite complex, but I thought that
there must be a simple conversion function that will take a standard
ctime()-generated string such as the above example and return the time.
Maybe I just missed it in the docs?


Lastly (for now), my basic regular expression parsing works like this:

    pattern = re.compile(" Log " + playerName + ": ")
    return [eachLine for eachLine in lines if pattern.search(eachLine)]

This obviously returns each line that matches "Log Whoever". But what if
I want to return the groups from the regular expression? For example, if
I wanted to just return the 'whoever' in the above situation, how would
I change the list comprehension to do this?

Any help appreciated.

--
Ben Sizer
http://pages.eidosnet.co.uk/kylotan






More information about the Python-list mailing list