pluvki
pluvki

Reputation: 115

How to count one specific word in 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. :)

Upvotes: 9

Views: 46322

Answers (6)

Hemo Syrai
Hemo Syrai

Reputation: 1

def Freq(x,y):
    d={}
    open_file = open(x,"r")
    lines = open_file.readlines()
    for line in lines:
        word = line.lower()
        words = word.split()
        for i in words:
            if i in d:
                d[i] = d[i] + 1
            else:
                d[i] = 1
    print(d)

Upvotes: 0

Narendra
Narendra

Reputation: 1

fi=open("text.txt","r")
cash=0
visa=0
amex=0
for line in fi:
    k=line.split()
    print(k)
    if 'Cash' in k:
        cash=cash+1
    elif 'Visa' in k:
        visa=visa+1
    elif 'Amex' in k:
        amex=amex+1

print("# persons paid by cash are:",cash)
print("# persons paid by Visa card are :",visa)
print("#persons paid by Amex card are :",amex)
fi.close()

Upvotes: -2

Eugene Yarmash
Eugene Yarmash

Reputation: 149746

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).

Upvotes: 20

Wajahat
Wajahat

Reputation: 1633

If you only care about one word then you do not need to create a dictionary to keep track of every word count. You can just iterate over the file line-by-line and find the occurrences of the word you are interested in.

#!/usr/bin/env python

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

wordcount=0
my_word="apple"
for line in logfile:
    if my_word in line.split():
        wordcount += 1

print my_word, wordcount

However, if you also want to count all the words, and just print the word count for the word you are interested in then these minor changes to your code should work:

#!/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
# print only the count for my_word instead of iterating over entire dictionary
my_word="apple"
print my_word, wordcount[my_word]

Upvotes: 7

Brendan Abel
Brendan Abel

Reputation: 37509

You can use the Counter dictionary for this

from collections import Counter

with open("log_file", "r") as logfile:
    word_counts = Counter(logfile.read().split())

print word_counts.get('apple')

Upvotes: 1

Yhlas
Yhlas

Reputation: 421

This is an example of counting words in array of words. I am assuming file reader will be pretty much similar.

def count(word, array):
    n=0
    for x in array:
        if x== word:
            n+=1
    return n

text= 'apple orange kiwi apple orange grape kiwi apple apple'
ar = text.split()

print(count('apple', ar))

Upvotes: 0

Related Questions