【发布时间】:2015-08-21 19:51:58
【问题描述】:
[使用 Python 3.3.3]
我正在尝试分析文本文件,清理它们,打印唯一单词的数量,然后尝试将唯一单词列表保存到文本文件中,每行一个单词,每个单词出现的次数清理后的单词列表。 所以我所做的是我拿了文本文件(哈珀总理的演讲),通过只计算有效的字母字符和单个空格来清理它,然后我计算了唯一单词的数量,然后我需要制作一个保存的文本文件的唯一词,每个唯一的词都在自己的行上,并在单词旁边,该单词在清理列表中的出现次数。这就是我所拥有的。
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 是文本文件的清理版本,只有单词和空格,没有其他内容。所以,我希望将 newWords 文件中的每个唯一单词保存到一个文本文件中,每行一个单词,并且在单词旁边,在 newWords 中具有该单词的出现次数(不在唯一单词列表中,因为这样每个单词都会有 1 次出现)。我的功能有什么问题?谢谢!
【问题讨论】:
-
你怎么知道它不起作用?
标签: python file python-3.x io text-files