Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python2
- import string
- import sys
- import collections
- import csv
- def main():
- """Counts words (and numbers) in a file"""
- try:
- infilename, outfilename = sys.argv[1:3]
- except IndexError:
- print "Usage: %s INFILE.txt OUTFILE.csv" % sys.argv[0]
- sys.exit(1)
- with open(infilename, 'r') as infile:
- text = infile.read()
- wordstring = text.translate(None, string.punctuation)
- wordlist = wordstring.split()
- counts = collections.Counter(wordlist)
- ordered = counts.most_common()
- with open(outfilename, 'w') as outfile:
- writer = csv.writer(outfile)
- writer.writerows(ordered)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement