Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.72 KB | None | 0 0
  1. import csv
  2.  
  3. # user input for the file
  4. fileName = input()
  5.  
  6. # dictionary to store words with their frequencies
  7. wordsFrequency = {}
  8.  
  9. # reading the file
  10. with open(fileName, 'r') as csvfile:
  11.     csvreader = csv.reader(csvfile)
  12. # iterating throught each row
  13.     for row in csvreader:
  14.         for word in row:
  15. # checking if word exixt in the dictionary or not if not present then add the word in the dictionary with frequency 1
  16.             if word not in wordsFrequency.keys():
  17.                 wordsFrequency[word] = 1
  18. # else increase the frequency by 1
  19.             else:
  20.                 wordsFrequency[word] += 1
  21.  
  22. # printin the result
  23. for key in wordsFrequency.keys():
  24.     print(key + " " + str(wordsFrequency[key]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement