faddai

wordy.py

Sep 15th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.44 KB | None | 0 0
  1. #!/usr/bin/python
  2. """filename: wordy.py"""
  3.  
  4. sample_file = open("sample.txt", "r")
  5.  
  6. words = sample_file.read().split()
  7. count_and_word = {}
  8.  
  9. for word in words:
  10.     word = word.replace(".","").replace("(", "").replace(")", "").replace(",", "").lower()
  11.  
  12.     if word not in count_and_word:
  13.         count_and_word[word] = 1
  14.     else:
  15.         count_and_word[word] += 1
  16.  
  17. for word in sorted(count_and_word.keys()):
  18.     print "{0}: {1}".format(count_and_word[word], word)
Advertisement
Add Comment
Please, Sign In to add comment