loop does not count...

J

Jonathan Driller

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
#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()
 
J

John Roth

Jonathan Driller said:
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
#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

to

check = urlList.strip()

and it might work better.



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

John Roth
 
T

Terry Reedy

Jonathan Driller said:
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
 
J

Jonathan Driller

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.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()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,468
Latest member
Fannie44U3

Latest Threads

Top