python - find the occurrence of the word in a file
from collections import Counter;
cnt = Counter ();
for line in open ('TEST.txt', 'r'):
for word in line.split ():
cnt [word] += 1
print cnt
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})