mraps98

Python_printWordsAndFrequenciesOfFile

May 22nd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.42 KB | None | 0 0
  1. import json
  2.  
  3. def wordFreq(text):
  4.     counts = dict()
  5.     words = text.split()
  6.     count = 0
  7.     for word in words:
  8.         if word in counts:
  9.             counts[word]+=1
  10.         else:
  11.             counts[word]=1
  12.     with open('words.txt','w') as fo:
  13.         fo.write(json.dumps(counts))
  14.        
  15.  
  16. filename = input("Enter file to analyze: ")
  17.  
  18. with open(filename, 'r') as fi:
  19.     text = fi.read()
  20.     wordFreq(text)
Add Comment
Please, Sign In to add comment