472,353 Members | 2,211 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 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 3557

"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...
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...
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...
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,...
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...
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...
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...
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...
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...
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...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.