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
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, ” “, item
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.