Count specific word in file python

Contents

  • Introduction
  • Syntax – count[]
  • Example 1: Count how many times a word occurred in given Text File
  • Summary

To count the number of occurrences of a specific word in a text file, read the content of text file to a string and use String.count[] function with the word passed as argument to the count[] function.

Syntax – count[]

Following is the syntax of count[] function.

n = String.count[word]

where word is the string, and count[] returns the number of occurrences of word in this String.

Example 1: Count how many times a word occurred in given Text File

In this example, we will consider the following text file, and count the number of occurrences of “python” word.

Text File

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.

Python Program

#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]

#read content of file to string
data = file.read[]

#get number of occurrences of the substring in the string
occurrences = data.count["python"]

print['Number of occurrences of the word :', occurrences]

Output

Number of occurrences of the word : 2

Summary

In this tutorial of Python Examples, we learned how to count number of occurrences of a word in a given string.

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. :]

Wajahat

1,5693 gold badges20 silver badges45 bronze badges

asked Jul 15, 2016 at 16:34

1

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

answered Jul 15, 2016 at 16:37

Eugene YarmashEugene Yarmash

134k37 gold badges309 silver badges366 bronze badges

0

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]

answered Jul 15, 2016 at 16:38

WajahatWajahat

1,5693 gold badges20 silver badges45 bronze badges

2

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']

answered Jul 15, 2016 at 16:57

Brendan AbelBrendan Abel

33.2k14 gold badges86 silver badges112 bronze badges

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]]

answered Jul 15, 2016 at 16:43

YhlasYhlas

3913 gold badges4 silver badges17 bronze badges

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]

answered Mar 21, 2018 at 9:34

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[]

answered Feb 6, 2018 at 20:12

1

How do you count certain words in a string in Python?

Below is the implementation of the above approach : C++ Java. Python 3..
First, we split the string by spaces in a..
Then, take a variable count = 0 and in every true condition we increment the count by 1..
Now run a loop at 0 to length of string and check if our string is equal to the word..

How do you count words in a text file?

Algorithm.
Open a file in read mode using file pointer..
Read a line from file..
Split the line into words and store it in an array..
Iterate through the array, increment count by 1 for each word..
Repeat all these steps till all the lines from the files has been read..

How do you count text in Python?

Python String count[] Method The count[] method returns the number of times a specified value appears in the string.

How do you count the number of times a word appears in a string Python?

count[] Python: Using Strings. The count[] method can count the number of occurrences of a substring within a larger string. The Python string method count[] searches through a string. It returns a value equal to the number of times a substring appears in the string.

Chủ Đề