[using Python 3.3.3]
I'm trying to analyse text files, clean them up, print the amount of unique words, then try to save the unique words list to a text file, one word per line with the amount of times each unique word appears in the cleaned up list of words. SO what i did was i took the text file (a speech from prime minister harper), cleaned it up by only counting valid alphabetical characters and single spaces, then i counted the amount of unique words, then i needed to make a saved text file of the unique words, with each unique word being on its own line and beside the word, the number of occurances of that word in the cleaned up list. Here's what i have.
def uniqueFrequency(newWords):
'''Function returns a list of unique words with amount of occurances of that
word in the text file.'''
unique = sorted(set(newWords.split()))
for i in unique:
unique = str(unique) + i + " " + str(newWords.count(i)) + "\n"
return unique
def saveUniqueList(uniqueLines, filename):
'''Function saves result of uniqueFrequency into a text file.'''
outFile = open(filename, "w")
outFile.write(uniqueLines)
outFile.close
newWords is the cleaned up version of the text file, with only words and spaces, nothing else. So, I want each unique word in the newWords file to be saved to a text file, one word per line, and beside the word, have the # of occurances of that word in newWords (not in unique words list because then each word would have 1 occurance). What is wrong with my functions? Thank you!
The line above, is appending at the end of the existing set - "unique", if you use some other variable name instead, like "var", that should return correctly.