Skip to content Skip to sidebar Skip to footer

Word Frequency In Python Not Working

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 lin

Solution 1:

This loop for line in openfile: moves the file pointer at the end of the file. So, if you want to read the data again then either move the pointer(openfile.seek(0)) to the start of the file or re-open the file.

To get the word frequency better use Collections.Counter:

from collections import Counter
withopen("total data", "r") as openfile:
   c = Counter()
   for line in openfile:
      words = line.split()
      c.update(words)

Solution 2:

Add openfile.seek(0) right after you initialize count. That'll put the read pointer to the beginning of the file

Solution 3:

This is a much more direct way of counting the word frequency in a file:

from collections import Counter

defcount_words_in_file(file_path):
    withopen(file_path) as f:
        return Counter(f.read().split())

Example:

>>> count_words_in_file('C:/Python27/README.txt').most_common(10)
[('the', 395), ('to', 202), ('and', 129), ('is', 120), ('you', 111), ('a', 107), ('of', 102), ('in', 90), ('for', 84), ('Python', 69)]

Post a Comment for "Word Frequency In Python Not Working"