I am trying to count frequencies of words in a text file using python.
I am using the following code:
openfile=open("total data", "r")
linecount=0
for line in openfile:
if line.strip():
linecount+=1
count={}
while linecount>0:
line=openfile.readline().split()
for word in line:
if word in count:
count[word]+=1
else:
count[word]=1
linecount-=1
print count
But i get an empty dictionary. "print count" gives {} as output
I also tried using:
from collections import defaultdict
.
.
count=defaultdict(int)
.
.
if word in count:
count[word]=count.get(word,0)+1
But i'm getting an empty dictionary again. I dont understand what am i doing wrong. Could someone please point out?