473,394 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

loop does not count...

I am very new to this and would greatly appreciate some insight.
I am trying to learn Python by doing something useful; write a script
that will count and output my aggregated visits to my website. I have
a separate text file that holds the list of uri strings that I want to
count and then this code. The log is sampleLog.txt.

The problem is that it says all the preceding uris are 0 (they are
not) and only the last string actually is counted....why is that?

def stats():
import sys
import string
#read the file of current urls - presumes it exists already
x = open('urlList.txt')
# note this reads as a file, not a list
urlFile = x.read()
# don't need to close but should
x.close()
#list what is in text file of urls
print "Here is what we check now:\n", urlFile
print "\n"

# len(listName) gives # of list elements

#turn url listings into list
z = open('urlList.txt')
urlList = z.readlines()
#open log file
log = open('sampleLog.txt')
logFile = log.read()
#initialize counter at 0
i = 0
# loop through to search for urls
while i < len(urlList):
# put element into var
check = urlList[i]
#print out # found and what it was
print check, " found" , string.count(logFile, check) ,"times
\n"
# increment for next item - can't do i ++
i = i + 1
z.close()
Jul 18 '05 #1
3 3602

"Jonathan Driller" <jd******@orchid.org> wrote in message
news:a8**************************@posting.google.c om...
I am very new to this and would greatly appreciate some insight.
I am trying to learn Python by doing something useful; write a script
that will count and output my aggregated visits to my website. I have
a separate text file that holds the list of uri strings that I want to
count and then this code. The log is sampleLog.txt.

The problem is that it says all the preceding uris are 0 (they are
not) and only the last string actually is counted....why is that?

def stats():
import sys
import string
#read the file of current urls - presumes it exists already
x = open('urlList.txt')
# note this reads as a file, not a list
urlFile = x.read()
# don't need to close but should
x.close()
#list what is in text file of urls
print "Here is what we check now:\n", urlFile
print "\n"

# len(listName) gives # of list elements

#turn url listings into list
z = open('urlList.txt')
urlList = z.readlines()
#open log file
log = open('sampleLog.txt')
logFile = log.read()
#initialize counter at 0
i = 0
# loop through to search for urls
while i < len(urlList):
# put element into var
check = urlList[i]
#print out # found and what it was
print check, " found" , string.count(logFile, check) ,"times
\n"
# increment for next item - can't do i ++
i = i + 1
z.close()


The lines you're reading from your test file all end
in a newline, so that may be the reason you're not
finding them in the log file. I suspect that you didn't
end your test file with a return, so that line was
found.

change

check = urlList[i]

to

check = urlList[i].strip()

and it might work better.

Also, your imports belong at the module level,
not inside the definition.

John Roth
Jul 18 '05 #2

"Jonathan Driller" <jd******@orchid.org> wrote in message
news:a8**************************@posting.google.c om...
I am very new to this
A common newbie programmer mistake, having read 'comment your code',
is to overcomment to the point of giving a parallel plain-text version
of the code, as you did. Your worse example of this is
#initialize counter at 0
i = 0


The result is to make the code harder and more painful to read,
especially for an experienced programmer, rather than easier, which is
the proper purpose of comments. If you are going to write a parallel
psuedocode version, make it truly parallel by using your space key (or
tab key in editor that converts to spaces) to put the comments over to
the right:

i = 0
#initialize counter at 0

Then someone can much more easily ignore the redundant comments to
read the actual code.

There is no need to read urlList.txt twice.
z = open('urlList.txt')
urlList = z.readlines()
can be replaced by
urlList = urlFile.split('\n')

Unless you need to write code for 1.5.2, I recommend you learn now to
write string methods as methods rather than as string module
functions. The latter is a redundant access path for backwards
compatibility that will probably disappear in the future. If you look
in the string module, you will find, for instance, def count(s,
*args): return s.count(*args), so all you are doing is wrapping the
method calls in an extra function call.

I hope Johm's answer is the one you needed.

Terry J. Reedy

Jul 18 '05 #3
Thanks.

That worked beautifully - except that I kept getting errors if I moved
the import statements out of the def... "invalid syntax"

So, now it is (with the changed comments per Terry's suggestion to be
added):
def stats():
import sys
import string
#read the file of current urls that are to be checked - presumes
it exists already
x = open('urlList.txt')
# note this reads as a file, not a list
urlFile = x.read()
# don't need to close but should
x.close()
#list what is in text file of urls
print "Here is what we check now:\n", urlFile
print "\n"
#prompt to add additional urls - use raw so quotes not nec
addItem = raw_input('Enter an additional URL in quotes:' )
# can't append() to a file, must list.append() or do this way
urlList = urlFile + "\n" + addItem
#write additional urls to list
y = open('urlList.txt', 'w')
y.write(urlList)
y.close()

# len(listName) gives # of list elements

#turn url listings into list
z = open('urlList.txt')
urlList = z.readlines()
#open log file
log = open('sampleLog.txt')
logFile = log.read()
#initialize counter at 0
i = 0
# loop through to search for urls
while i < len(urlList):
# put element into var
check = urlList[i].strip()
#print out # found and what it was
print check, " found" , string.count(logFile, check) ,"times
\n"
# increment for next item - can't do i ++
i = i + 1
z.close()
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Anand Pillai | last post by:
This is for folks who are familiar with asynchronous event handling in Python using the asyncore module. If you have ever used the asyncore module, you will realize that it's event loop does not...
6
by: Ravi | last post by:
Hi All, I am trying to execute a select statement using the DBI module of perl in a for loop. I am getting a strange behaviour, the select statement is excuting correctly only for the last element...
4
by: hall | last post by:
Hi. I've come across someting strange. I was trying to make a for-loop execute repetadly until the function called inside it does not return true during the entire loop (see program below). ...
8
by: Dave Veeneman | last post by:
In a for-loop, is a calculated expression re-calculated on each pass through the loop, or only once, when the loop is initialized? For example, assume the following loop: for (int i = 0; i <...
8
by: Nancy | last post by:
Greetings: First, I apologize if my posting format is improper. The code below does what I intended it to do, but presently only displays 1 table entry. I've grown it to this point, but really...
102
by: tom fredriksen | last post by:
Hi I was doing a simple test of the speed of a "maths" operation and when I tested it I found that removing the loop that initialises the data array for the operation caused the whole program to...
16
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
13
by: Rick | last post by:
The following code will enter an infinate loop when in ReadChars. I can only make it happen when reading a Stream and with this particular XML. If I use the ReadInnerXml call rather than my own...
3
by: Brad | last post by:
Hi folks, I'm still fairly new to programming in python and programming in general. A friend of mine is in a CompSci 101 course and was working on a slider game when he encountered a problem. We...
23
by: tshad | last post by:
Is there a way to know if you are looking at the last record record of foreach loop other then setting up a loop counter that you manually increment? foreach (Racecar racecar in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.