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:
TEST.txt
ashwin prog
Using a Defaultdict:
from collections import defaultdict def read_file(fname): words_dict = defaultdict(int) fp = open(fname, 'r') lines = fp.readlines() words = [] for line in lines: words += line.split(' ') for word in words: words_dict[word] += 1 return words_dict