I am using python to parse server access log file entries. I have a need to skip over the first four lines in the file, and not use them as input to my parsing, however it doesn't seem to be working the way I am trying.
Here is some code -
for eachLine in fileHandle: # Skip the first four lines of the file, which contain column # headers, and other non-log entry information. if loopCount <= 5: loopCount += 1 continue # Split each line into columns on space character separators. columns = eachLine.split(" ") if len(columns) < 22: continue if columns == "#Fields": continue
The only code that should be required to skip those first four lines of the log file is the first if statement in the code above, and the other if's I have added in desperation in an attempt to insure that those lines do not get fed to the parsing routines.
Why is the continue not going to the top of the loop, and starting the next loop here? As you can see from the following, the value of one of those lines is being used as an eachLine value, and is messing up the parsing later -
Building Visitors lists. date - 2009-04-14 date - 2009-04-15 date - #Fields: Traceback (most recent call last): File "/home/ray/LogAnalyzer/iis-log-analyze.py", line 2636, in <module> VisitorCounter, TotalsCounter = ReadInDaysLog(tempLogName, countryCodes) File "/home/ray/LogAnalyzer/iis-log-analyze.py", line 2315, in ReadInDaysLog year, month, day = date.split("-") ValueError: need more than 1 value to unpack
The value of date should not ever be assigned #Fields as a value since it is from one of those first four lines in the file, Can anyone tell me why it keeps getting assigned to date, when the first block of code should be preventing it that line from ever being parsed?
It was my understanding that the continue command breaks out of the current loop, and goes to the top of the loop to continue with the next value for the loop, but it doesn't seem to be working for me.
EDIT: I changed the if condition from (loopCount <= 5) to (loopCount < 4), and the code works a bit better now. I was not only skipping the first four lines [zero through 3], I was also skipping lines 5, and 6 [index 4, and 5] since I started the loopCount at zero, and increment it each time through the loop if it passes the if clause.
Edited by Ray Parrish, 08 April 2010 - 03:39 PM.