New to Python.

Nobody user_77 at hotmail.com
Thu Mar 18 04:44:09 CET 2004


How 'bout this:

import string

f=open ('Bob.txt','r')

counter = 0

for line in f.readlines():

   word = string.split(line)

   for x in word:

      if x == 'customer':

        counter = counter + 1

print 'Found it ' + str(counter)





"droog" <droog at orange.gov> wrote in message
news:9k4i50dnfsi42gdr5rue1vnpq7gfqkanb6 at 4ax.com...
> Thanks for helping, and no this is not a homework assignment
> I just like python and wanted to learn it on my own.
> droog
>
> On Thu, 18 Mar 2004 03:07:05 GMT, droog <droog at orange.gov> wrote:
>
> >I was looking for the simpliest way of doing it. This is what I tried
> >to do.
> >
> >f = open('C:\Python23\Samples\Bob.txt', 'r')
> >counter = 0
> >while True:
> > line = f.readline()
> > if len(line) == 0:
> > break
> > if line.find('customer'):
> > counter = counter + 1
> > print 'Found it ' + str(counter)
> >
> >
> >
> >On Thu, 18 Mar 2004 02:53:48 GMT, droog <droog at orange.gov> wrote:
> >
> >>
> >>Hello!
> >>
> >>I have just started learning python and encountered a problem.
> >>All I wanted to do, was to open a text file search and count the
> >>number of occurances of a single word and print that count.
> >>Thanks for help.
>
>





More information about the Python-list mailing list