简体   繁体   中英

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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM