Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python - find the occurrence of the word in a file

I am trying to find the count of words that occured in a file. I have a text file (TEST.txt) the content of the file is as follows:

ashwin programmer india
amith programmer india

The result I expect is:

{ 'ashwin':1, 'programmer ':2,'india':2, 'amith ':1}

The code I am using is:

for line in open(TEST.txt,'r'):
    word = Counter(line.split())
    print word

The result I get is:

Counter({'ashwin': 1, 'programmer': 1,'india':1})
Counter({'amith': 1, 'programmer': 1,'india':1})

Can any one please help me? Thanks in advance .

like image 838
Ashwin Avatar asked Feb 26 '13 06:02

Ashwin


People also ask

How do you count the occurrence of a word in a file in Python?

Python Code:def word_count(str): counts = dict() words = str. split() for word in words: if word in counts: counts[word] += 1 else: counts[word] = 1 return counts print( word_count('the quick brown fox jumps over the lazy dog. '))

How do you find the occurrence in Python?

Operator. countOf() is used for counting the number of occurrences of b in a. It counts the number of occurrences of value. It returns the Count of a number of occurrences of value.

How do you find the occurrence of a word in a string?

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


2 Answers

You're iterating over every line and calling Counter each time. You want Counter to run over the entire file. Try:

from collections import Counter

with open("TEST.txt", "r") as f:
    # Used file context read and save into contents
    contents = f.read().split()
print Counter(contents)
like image 180
Anorov Avatar answered Oct 12 '22 23:10

Anorov


Use the update method of Counter. Example:

from collections import Counter

data = '''\
ashwin programmer india
amith programmer india'''

c = Counter()
for line in data.splitlines():
    c.update(line.split())
print(c)

Output:

Counter({'india': 2, 'programmer': 2, 'amith': 1, 'ashwin': 1})
like image 32
Mark Tolonen Avatar answered Oct 12 '22 22:10

Mark Tolonen