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.
On Thu, 18 Mar 2004 02:53:48 GMT, droog wrote:[color=blue]
> 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.[/color]
Can you show us code that you've tried that fails to do what you want?
--
\ "I never forget a face, but in your case I'll be glad to make |
`\ an exception." -- Groucho Marx |
_o__) |
Ben Finney <http://bignose.squidly .org/>
In article <sg3i50l1utn3dr vqaggrm2rqf7n2e rbu8h@4ax.com>,
droog <droog@orange.g ov> wrote:
[color=blue]
> 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.
>[/color]
Well, I'll point you in the right direction.
To open a file, use the "open" function. You can loop through the lines
with something like "for line in file:". Depending on exactly how you
define "word", you might want to look at the split() string method, or
maybe some regular expression matching with the "re" module.
There, that should be enough to get you started with your homework
assignment :-)
I was looking for the simpliest way of doing it. This is what I tried
to do.
f = open('C:\Python 23\Samples\Bob. txt', 'r')
counter = 0
while True:
line = f.readline()
if len(line) == 0:
break
if line.find('cust omer'):
counter = counter + 1
print 'Found it ' + str(counter)
On Thu, 18 Mar 2004 02:53:48 GMT, droog <droog@orange.g ov> wrote:
[color=blue]
>
>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.[/color]
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@orange.g ov> wrote:
[color=blue]
>I was looking for the simpliest way of doing it. This is what I tried
>to do.
>
>f = open('C:\Python 23\Samples\Bob. txt', 'r')
>counter = 0
>while True:
> line = f.readline()
> if len(line) == 0:
> break
> if line.find('cust omer'):
> counter = counter + 1
> print 'Found it ' + str(counter)
>
>
>
>On Thu, 18 Mar 2004 02:53:48 GMT, droog <droog@orange.g ov> wrote:
>[color=green]
>>
>>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.[/color][/color]
In article <h84i501homhvjv 6a4ohmf0o4svlak cqtoa@4ax.com>,
droog <droog@orange.g ov> wrote:
[color=blue]
> I was looking for the simpliest way of doing it. This is what I tried
> to do.
>
> f = open('C:\Python 23\Samples\Bob. txt', 'r')
> counter = 0
> while True:
> line = f.readline()
> if len(line) == 0:
> break
> if line.find('cust omer'):
> counter = counter + 1
> print 'Found it ' + str(counter)[/color]
I cut-and-pasted the above onto my machine (just changed the pathname to
/usr/share/dict/words). It finds every line in the file! The problem
appears to be that the find() method returns -1 when there's no match,
which tests as True. Other than that, the code seems pretty reasonable.
Could you please show me an alternative and better way of doing it?
tia
On Wed, 17 Mar 2004 22:22:41 -0500, Roy Smith <roy@panix.co m> wrote:
[color=blue]
>In article <h84i501homhvjv 6a4ohmf0o4svlak cqtoa@4ax.com>,
> droog <droog@orange.g ov> wrote:
>[color=green]
>> I was looking for the simpliest way of doing it. This is what I tried
>> to do.
>>
>> f = open('C:\Python 23\Samples\Bob. txt', 'r')
>> counter = 0
>> while True:
>> line = f.readline()
>> if len(line) == 0:
>> break
>> if line.find('cust omer'):
>> counter = counter + 1
>> print 'Found it ' + str(counter)[/color]
>
>I cut-and-pasted the above onto my machine (just changed the pathname to
>/usr/share/dict/words). It finds every line in the file! The problem
>appears to be that the find() method returns -1 when there's no match,
>which tests as True. Other than that, the code seems pretty reasonable.[/color]
In article <dm5i501ccv5atb e4mc6epn1l406ug ufkto@4ax.com>,
droog <droog@orange.g ov> wrote:
[color=blue]
> Thanks Roy!
>
> Could you please show me an alternative and better way of doing it?
> tia
>
> On Wed, 17 Mar 2004 22:22:41 -0500, Roy Smith <roy@panix.co m> wrote:
>[color=green]
> >In article <h84i501homhvjv 6a4ohmf0o4svlak cqtoa@4ax.com>,
> > droog <droog@orange.g ov> wrote:
> >[color=darkred]
> >> I was looking for the simpliest way of doing it. This is what I tried
> >> to do.
> >>
> >> f = open('C:\Python 23\Samples\Bob. txt', 'r')
> >> counter = 0
> >> while True:
> >> line = f.readline()
> >> if len(line) == 0:
> >> break
> >> if line.find('cust omer'):
> >> counter = counter + 1
> >> print 'Found it ' + str(counter)[/color]
> >
> >I cut-and-pasted the above onto my machine (just changed the pathname to
> >/usr/share/dict/words). It finds every line in the file! The problem
> >appears to be that the find() method returns -1 when there's no match,
> >which tests as True. Other than that, the code seems pretty reasonable.[/color]
>[/color]
The basic code you've got seems reasonable, other than the find() bug.
Perhaps not the cleanest or most efficient, but reasonable. Try
changing the test to something like "if line.find ('customer') != -1:"
and see what happens.
If you wanted to be faster, I'd look at the "re" (regular expression)
module. You could do something like:
(outside your mail loop)
prog = re.compile ('customer')
then the test inside your loop would be something:
if prog.match (line):
Note: I'm doing this from memory, so I may have messed up the details a
bit. Check out the documentation for the re module. Unfortunately,
it's one of the more complex modules, so the documentation may be a bit
obtuse to somebody just getting into the language.
Now that I think about it, given that you're looking for a constant
string, I'm not even sure using re will be any faster. But try it both
ways and time it to find out!
"droog" <droog@orange.g ov> wrote in message
news:9k4i50dnfs i42gdr5rue1vnpq 7gfqkanb6@4ax.c om...[color=blue]
> 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@orange.g ov> wrote:
>[color=green]
> >I was looking for the simpliest way of doing it. This is what I tried
> >to do.
> >
> >f = open('C:\Python 23\Samples\Bob. txt', 'r')
> >counter = 0
> >while True:
> > line = f.readline()
> > if len(line) == 0:
> > break
> > if line.find('cust omer'):
> > counter = counter + 1
> > print 'Found it ' + str(counter)
> >
> >
> >
> >On Thu, 18 Mar 2004 02:53:48 GMT, droog <droog@orange.g ov> wrote:
> >[color=darkred]
> >>
> >>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.[/color][/color]
>
>[/color]
At 2004-03-18T03:07:05Z, droog <droog@orange.g ov> writes:
[color=blue]
> if line.find('cust omer'):[/color]
Note that this method, when fixed, will only count the number of lines where
'customer' occurs, regardless of the number of times it can be found on each
line.
A (slightly) more accurate counter would be:
counter += line.count('cus tomer')
- --
Kirk Strauser
The Strauser Group
Open. Solutions. Simple.
"droog" <droog@orange.g ov> wrote in message
news:dm5i501ccv 5atbe4mc6epn1l4 06ugufkto@4ax.c om...[color=blue]
> Could you please show me an alternative and better way of doing it?
> tia[/color]
# if the file is not very large you can slurp and count
source = file('C:\Python 23\Samples\Bob. txt')
tally = source.read().c ount("customer" )
source.close()
# otherwise, you can go line by line
source = file('C:\Python 23\Samples\Bob. txt')
tally = 0
for line in source:
if "customer" in line:
tally += 1
source.close()
Note: "customer" matches as a substring. It will match "customers" but not
"Customer", that sort of thing. Depending on how you want to count matches,
you may need to do further text processing (like converting the line to
lower case before matching), or use a regular expression for precise
matching (such as not counting "customers" , when you only want to count
"customer") .
"Sean Ross" <sross@connectm ail.carleton.ca > wrote in message
news:m3a6c.1955 4$E71.1405188@n ews20.bellgloba l.com...
[snip][color=blue]
> # otherwise, you can go line by line
> source = file('C:\Python 23\Samples\Bob. txt')
> tally = 0
> for line in source:
> if "customer" in line:
> tally += 1
> source.close()
>[/color]
[snip]
# sorry, that should be
source = file('C:\Python 23\Samples\Bob. txt')
tally = 0
for line in source:
tally += line.count("cus tomer")
source.close()
Sean Ross wrote:
[color=blue]
> # sorry, that should be
> source = file('C:\Python 23\Samples\Bob. txt')[/color]
You really want to use double backslashes (\\) here or use a raw string
(r'...'). If not, this will eventually bite you.
--
__ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ Pick the roses from the thorns / Wash with waters of the storms
-- Chante Moore
"Erik Max Francis" <max@alcyone.co m> wrote in message
news:40593E14.E AF16BD5@alcyone .com...[color=blue]
> Sean Ross wrote:
>[color=green]
> > # sorry, that should be
> > source = file('C:\Python 23\Samples\Bob. txt')[/color]
>
> You really want to use double backslashes (\\) here or use a raw string
> (r'...'). If not, this will eventually bite you.[/color]
Or forward slashes (/), which avoids the whole problem.
Comment