Skip to content

Instantly share code, notes, and snippets.

@zain
Created June 14, 2011 22:02
Show Gist options
  • Save zain/1026036 to your computer and use it in GitHub Desktop.
Save zain/1026036 to your computer and use it in GitHub Desktop.
Word counter
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