python - find the occurrence of the word in a file

前端 未结 6 424
自闭症患者
自闭症患者 2020-12-03 22:04

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:

ashwin prog         


        
6条回答
  •  天涯浪人
    2020-12-03 22:24

    f = open('input.txt', 'r')
    data=f.read().lower()
    list1=data.split()
    
    d={}
    for i in set(list1):
        d[i]=0
    
    for i in list1:
        for j in d.keys():
           if i==j:
              d[i]=d[i]+1
    print(d)
    

提交回复
热议问题