我正在编写一个程序来计算 txt 文件中出现次数最多的 20 个单词。当我将它剥离并计数一个文件时,该程序运行良好,但是当我输入两个要剥离和计数的文件时,我收到一条错误消息,指出“'Counter' 对象不可调用”。我很困惑,因为这在一个文档中也能正常工作。下面是我的代码,错误来自 while 循环。谢谢!
from collections import Counter
numOfData = int(input("How many documents would you liek to scan? "))
i = 0
displayedI = str(i)
docList = []
finalData = []
##Address of document would take 'test.txt' for example
while i < numOfData:
newAddress = input("Address of document " + displayedI + ": ")
docList.append(newAddress)
i += 1
print(docList)
indexList = 0
for x in docList:
file = open(docList[indexList], 'r')
data_set = file.read().strip()
file.close()
split_set = data_set.split()
##This is where the error is occurring
Counter = Counter(split_set)
most_occuring = Counter.most_common(20)
finalData.append(most_occuring)
indexList += 1
print(finalData)
四季花海
相关分类