Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count one specific word in Python?

Tags:

python

I want to count a specific word in the file.

For example how many times does 'apple' appear in the file. I tried this:

#!/usr/bin/env python
import re 

logfile = open("log_file", "r") 

wordcount={}
for word in logfile.read().split():
    if word not in wordcount:
        wordcount[word] = 1
    else:
        wordcount[word] += 1
for k,v in wordcount.items():
    print k, v

by replacing 'word' with 'apple', but it still counts all possible words in my file.

Any advice would be greatly appreciated. :)

like image 941
pluvki Avatar asked Jul 15 '16 16:07

pluvki


1 Answers

You could just use str.count() since you only care about occurrences of a single word:

with open("log_file") as f:
    contents = f.read()
    count = contents.count("apple")

However, to avoid some corner cases, such as erroneously counting words like "applejack", I suggest that you use a regex:

import re

with open("log_file") as f:
    contents = f.read()
    count = sum(1 for match in re.finditer(r"\bapple\b", contents))

\b in the regex ensures that the pattern begins and ends on a word boundary (as opposed to a substring within a longer string).

like image 79
Eugene Yarmash Avatar answered Nov 15 '22 20:11

Eugene Yarmash