Created
June 14, 2011 22:02
-
-
Save zain/1026036 to your computer and use it in GitHub Desktop.
Word counter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import operator, string | |
f = open('input.txt', 'r+') | |
s = f.read() | |
f.close() | |
just_words = s.lower().translate(string.maketrans("",""), string.punctuation) | |
counts = {} | |
for word in just_words.split(): | |
if word in counts: | |
counts[word] += 1 | |
else: | |
counts[word] = 1 | |
freq = sorted(counts.iteritems(), key=operator.itemgetter(1), reverse=True) | |
f = open('output.csv', 'w+') | |
for word, count in freq: | |
f.write("%s,%s\n" % (word, count)) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment