Re: loop does not count...

From: John Roth (newsgroups_at_jhrothjr.com)
Date: 11/13/03


Date: Wed, 12 Nov 2003 19:22:07 -0500


"Jonathan Driller" <jdriller@orchid.org> wrote in message
news:a8cff9fb.0311121513.521ca948@posting.google.com...
> 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



Relevant Pages

  • how can i get the current user which starts the store eventsink???
    ... i have built a script with the EventSink Wizard from the Exchange SDK ... Sub EventLog(EventName As String, ByVal bstrURLItem As String, ... *Dim objSession As MAPI.Session ... LogFile 'Set the path of the log file to be in the Windows System ...
    (microsoft.public.exchange.development)
  • Re: currentdb.execute
    ... Sub subDisplayAndLogError(ProcName As String, ErrNo As Long, Description As String, ParamArray DataItems() As Variant) ... ' log file as lines in the form "name = value". ... Dim strFileName As String ... Dim intLogFile As Integer ...
    (microsoft.public.access.formscoding)
  • Re: Help to identify different strings
    ... I advise against importing the log file into a "Table1" with one record ... Dim strLine As String ... I agree - it is easy to parse each individual line in Table1 by applying ... individual alarm type, then check whether this string is available in the ...
    (microsoft.public.access.modulesdaovba)
  • Script to Email Confirmation if Latest File Contains Specific String
    ... script which does the following: ... If a backup was successful, ... If that file contains the string '100.00', ... log file, as well as a confirmation such as 'Backup likely successful'. ...
    (microsoft.public.windows.server.scripting)
  • Re: Creating a User login Log file for an Excel workbook
    ... This could be a problem if the user opens the workbook, ... "GetComputerNameA" (ByVal lpBuffer As String, ... Dim lngLen As Long, lngX As Long ... I would like to create a simple log file for a shared workbook I have. ...
    (microsoft.public.excel.programming)