import string wordcount = {} exclude = set(string.punctuation) file = open("lyrics.txt", "r") for line in file: formattedLine = string.lower(line) #lowercase formattedLine = formattedLine.replace("\n", "") #remove new line formattedLine = ''.join(ch for ch in formattedLine if ch not in exclude) #remove punctuation #Count occurances try: wordcount[formattedLine] = wordcount[formattedLine] + 1 except KeyError: wordcount[formattedLine] = 1 #Sort the values words = [ (value, key) for key, value in wordcount.iteritems() ] words.sort() words.reverse() #Print results for value, key in words: print str(value) + " - " + key