OneMoreError
OneMoreError

Reputation: 7728

Storing the count of each word occurring in a text file

I want to store the count of each word occurring in a text file in a dictionary. I mean

fob= open('D:/project/report.txt','r')

I am able to store the lines into a list, but I need to split these lines into individual words and finally store their count (like in a ditionary).

lst=fob.radlines()

#This doesn't work, gives error
#AttributeError: 'list' object has no attribute 'split' 
mylst=lst.split()

How can I do that ? And what's the efficient way to do this ?

Upvotes: 1

Views: 218

Answers (1)

jamylak
jamylak

Reputation: 133554

For Python 2.7+

from collections import Counter

with open('D:/project/report.txt','r') as fob:
    c = Counter(word for line in fob for word in line.split())

For Python 2.5+

from collections import defaultdict
dd = defaultdict(int)

with open('D:/project/report.txt','r') as fob:
    for line in fob:
        for word in line.split():
            dd[word] += 1

For older pythons or people that hate defaultdict

d = {}

with open('D:/project/report.txt','r') as fob:
    for line in fob:
        for word in line.split():
            d[word] = d.get(word, 0) + 1

Upvotes: 1

Related Questions