New to Python.

Bryan Weingarten bryan.weingarten at mpgedit.org
Wed Mar 17 23:24:23 EST 2004


Nobody wrote:

> 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)
> 
> 
> 
> 
How about this?


How about this?

counter = 0
for line in file('myfile'):
     counter += line.split().count('customer')
print 'Found it', counter



or if the file is relatively small:

print 'Found it', file('myfile').read().split().count('customer')




More information about the Python-list mailing list