属性错误-int对象没有属性键-Python
我正在运行这段代码,但我似乎一直收到一个属性错误,我不知道如何修复它。在运行shell窗口时,我包括了代码和shell窗口属性错误-int对象没有属性键-Python,python,Python,我正在运行这段代码,但我似乎一直收到一个属性错误,我不知道如何修复它。在运行shell窗口时,我包括了代码和shell窗口 # the Count class. The wordleFromObject function takes a Count object as # input, and calls its getTopWords method. import string class Count: # method to initialize any data struct
# the Count class. The wordleFromObject function takes a Count object as
# input, and calls its getTopWords method.
import string
class Count:
# method to initialize any data structures, such as a dictionary to
# hold the counts for each word, and a list of stop words
def __init__(self):
#print("Initializing Word Counter")
# set the attrbute wordCounts to an empty dictionary
self.wordCounts = {}
infile = open("stop_words.txt", "r")
self.stop_word_dict = {};
for line in infile.readlines():
self.stop_word_dict = 1
# method to add one to the count for a word in the dictionary.
# if the word is not yet in the dictionary, we'll need to add a
# record for the word, with a count of one.
def incCount(self,word):
my_table = str.maketrans('', '', string.punctuation)
self.wordCounts = {}
if word in self.stop_word_dict.keys():
return
else:
self.stop_word_dict += 1
cleaned_word = word.translate(my_table).lower()
if cleaned_word != '':
if cleaned_word in self.wordCounts.keys():
self.wordCounts[cleaned_word] += 1
else:
self.wordCounts[cleaned_word] = 1
# method to look up the count for a word
def lookUpCount(self, word):
return self.wordCounts.get(word.lower(), 0)
def main():
print("Initializing Word Counter")
filename = input("Enter book file:")
infile = open(filename, "r")
counter = Count()
for line in infile.readlines():
words = [word.strip() for word in line.strip().split()]
for word in words:
counter.incCount(word)
infile.close()
# Test code for Part 2 and 3
# Comment this code once you have completed part 3.
print(counter.lookUpCount("alice"))
print(counter.lookUpCount("rabbit"))
print(counter.lookUpCount("and"))
print(counter.lookUpCount("she"))
return
# Test code for Part 4 and 5
# topTen = counter.getTopWords(10)
# print(topTen)
# Test code for Part 5
# Import the wordle module and uncomment the call to the wordle function!
# wordle.wordleFromObject(counter,30)
# run the main program
main()
错误消息:
Initializing Word Counter
Enter book file:Alice.txt
Traceback (most recent call last):
line 69, in <module>
main()
line 50, in main
counter.incCount(word)
line 28, in incCount
if word in self.stop_word_dict.keys():
AttributeError: 'int' object has no attribute 'keys'
初始化字计数器
输入图书文件:Alice.txt
回溯(最近一次呼叫最后一次):
第69行,在
main()
第50行,总机
计数器计数(字)
第28行,以incCount为单位
如果单词在self.stop\u word\u dict.keys()中:
AttributeError:“int”对象没有属性“keys”
在这一行中,您将stop_word_dict从dict更改为int,然后在代码中,您将尝试访问字典“keys”属性,您将
stop_word_dict
视为dict和int(self.stop_word_dict=1
)。决定你想要它是什么。这一行:self.stop\u word\u dict=1
是问题所在。如果文件每行仅包含一个单词,则可以执行:self.stop\u word\u dict[line.strip()]=self.stop\u word\u dict.get(line.strip(),0)+1
I执行此解决方案:self.stop\u word\u dict[line.strip()]=self.stop\u word\u dict.get(line.strip(),0)+1但收到一个错误,表示get()中的行未定义@SayandipDutta@AnRe行号?@SayandipDutta对不起,我把自己的代码弄糊涂了。第31行是我遇到问题的地方,(if-else语句),因为我按照您的建议更改了代码,所以我不确定在if,else语句中放什么。希望这是有意义的。
for line in infile.readlines():
self.stop_word_dict = 1