Main index > Python programming > parsing log file

By cs-cam (Desklet Author), on Fri Apr 15 02:48:31 2005: parsing log file.

Okay, I'm mentally retarded when it comes to python, I've been playing with this for way more time than I have free and it's driving me crazy! I wanna parse a log for some info to use, sounds easy but it's not. I used to do a bit of PHP and I know I could do it super easy with that so this is driving me crazy that it doens't just work. I'm obviously doing something wrong somewhere I just can't figure out where.

I'm using code something like this, I want to get a list of what was installed and when.

Code:


import re

r = open('/var/log/pacman.log')
log = r.read()
r.close()
exp = re.compile('\[(.+)\] installed (.+)', re.MULTILINE)
prnt exp.match(log)


Every time it prints 'None" which isn't what I'm after. Basic debugging says log contains the data I'm chasing. Here's some sample data from my log, just so you know what I'm working with.

Code:


[03/31/05 12:56] installed envision (CVS_20050330-1)
[03/31/05 12:56] installed xmms2 (050327-3)
[03/31/05 12:56] installed ruby-efl (CVS_20050330-1)
[03/31/05 12:56] installed euphoria (CVS_20050330-1)
[03/31/05 12:56] installed evidence (CVS_20050330-1)
[03/31/05 12:56] installed ruby (1.8.2-4)
[03/31/05 12:59] installed gnome-bluetooth (0.5.1-1)
[03/31/05 13:00] upgraded evas (CVS_20050330-1 -> CVS_20050330-1)
[03/31/05 13:18] installed libast (0.6-1)




Any tips would be greatly appreciated.

Thanks :)

By syfou (Core Developer & Desklet Author), on Fri Apr 15 03:55:52 2005.

Code:


import re
exp = re.compile('\[([^\]]+)\]\s+installed\s+(\w+)\s+\(([^\)]+)\)')
print [exp.match(line).groups() for line in file('pacman.log').readlines()
       if exp.match(line)]


Returns, on your input snipset:

Code:


[('03/31/05 12:56', 'envision', 'CVS_20050330-1'), ('03/31/05 12:56', 'xmms2', '050327-3'), ('03/31/05 12:56', 'euphoria', 'CVS_20050330-1'), ('03/31/05 12:56', 'evidence', 'CVS_20050330-1'), ('03/31/05 12:56', 'ruby', '1.8.2-4'), ('03/31/05 13:18', 'libast', '0.6-1')]



adesklets is proud to be hosted on:

SourceForge.net Logo

Back to adesklets.sf.net.