Advertisement
Guest User

Words Count

a guest
Oct 27th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  1. from string import ascii_letters
  2.  
  3.  
  4. def read_file(file_path):
  5.     f = open(file_path, 'r')
  6.     return f.read()
  7.  
  8.  
  9. def remove_symbols(InputString):
  10.     return "".join([ch for ch in InputString if ch in (ascii_letters + " ")])
  11.  
  12.  
  13. def print_count_words(string):
  14.     words_in_file = remove_symbols(string)
  15.     words_in_file = words_in_file.split(" ")
  16.     word_count = {}
  17.     for word in words_in_file:
  18.         if word_count.get(word) is not None:
  19.             word_count[word] += 1
  20.         else:
  21.             word_count[word] = 1
  22.     for key in word_count:
  23.         print key + '=' + str(word_count[key])
  24.  
  25.  
  26. def main():
  27.     file = read_file(r'C:\Users\Administrator\Desktop\wordcount_s6SrnKS\alice.txt')
  28.     print print_count_words(file)
  29.  
  30.  
  31. if __name__ == '__main__':
  32.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement