A Word Count Program (Python)

Hey guys, here is an interesting program that I wrote that inputs a text file and records all the occurrences of words in the text file. Pretty much a simple word counter.

from operator import itemgetter
import string

print "\t Welcome to the wordcounter program!\n\t please select a file to input\n\t:==>"

except Exception, e:
print "File not Found!"

for currentLine in textFile:
for w in words:#looping through the tokens
if w!="" or w!=" ":#Ignores white spaces
if w in wordcount:#if the current word is already in there, increment
if w not in wordcount:#if not create a new key value pair

#Returns a list of tupeles with sorted key value pairs

print”\t Here are a list of all the words in this Text”

#Gets each item in the list (which are tuples) and prints them
for item in wordcount:
print item[0], ” “, item[1]

I know the code format is very crappy. I still haven’t figured out how to post code snippets on WordPress (the <code> tag doesn’t do anything…)

Remember to add the tabs python requires if you try to run this code. It’s pretty cool

Ask questions below.


Tell me what you think!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s