469,303 Members | 2,047 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,303 developers. It's quick & easy.

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 3483

"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Ravi | last post: by
4 posts views Thread by hall | last post: by
8 posts views Thread by Dave Veeneman | last post: by
8 posts views Thread by Nancy | last post: by
102 posts views Thread by tom fredriksen | last post: by
3 posts views Thread by Brad | last post: by
23 posts views Thread by tshad | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.