Stev0
January 29th, 2010, 06:39 AM
Messing around trying to write myself a rudimentary log parser to learn how to use regular expressions in Python. I've got it working...for the most part. I was able to get it to parse a log file and output the desired expression, but when I entered an else statement into the function in order to give the program the ability to display an error, I broke it.
I've played with different positioning of the else statement, and it either doesn't parse the log file and just prints the error function, or it will parse like normal, until I purposely introduce an error, and just print error function continuously...any ideas what I'm doing wrong? And seeing as I have a total of about 45 minutes time programming in Python, is there a simpler or cleaner way of doing what I want it to do? Code is as follows:
import sys
import re
def fail():
print "Expression not found."
print ""
parser();
def parser():
log = raw_input ("Path to log file: ")
expr = raw_input ("Expression to parse: ")
r = re.compile(expr)
fd = open(log, 'r')
for i in fd:
if r.search(i):
print i
else:
fail();
parser();
I've played with different positioning of the else statement, and it either doesn't parse the log file and just prints the error function, or it will parse like normal, until I purposely introduce an error, and just print error function continuously...any ideas what I'm doing wrong? And seeing as I have a total of about 45 minutes time programming in Python, is there a simpler or cleaner way of doing what I want it to do? Code is as follows:
import sys
import re
def fail():
print "Expression not found."
print ""
parser();
def parser():
log = raw_input ("Path to log file: ")
expr = raw_input ("Expression to parse: ")
r = re.compile(expr)
fd = open(log, 'r')
for i in fd:
if r.search(i):
print i
else:
fail();
parser();