Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Getting continue to work in a loop.


  • Please log in to reply
1 reply to this topic

#1 Ray Parrish

Ray Parrish

  • Members
  • 91 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Cottage Grove, Oregon
  • Local time:02:44 PM

Posted 08 April 2010 - 10:13 AM

Hello,

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[0] == "#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.


BC AdBot (Login to Remove)

 


#2 PropagandaPanda

PropagandaPanda


  • Malware Response Team
  • 10,433 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:06:44 PM

Posted 08 April 2010 - 12:33 PM

Hello Ray.

Might seem obvious, but have you initialized loopCount to 1?

Also, could you provide a sample of the first 10 or so lines of a file you are parsing? It would help in debugging.

With Regards,
The Panda




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users