【发布时间】:2021-10-17 09:03:35
【问题描述】:
我试图使用代码用空白替换其他所有内容:
corpus_test = []
ps = PorterStemmer()
for i in range(len(texts)):
review = re.sub('[^a-zA-Z]',' ',texts['title'][i])
review = review.lower()
review = review.split()
review = [ps.stem(word) for word in review if not word in stopwords.words('english')]
review = ' '.join(review)
corpus_test.append(review)
但我收到以下错误:
密钥错误:53
这来自于re模块的使用:
【问题讨论】:
-
你的 texts 变量是什么样的?
-
请将整个堆栈跟踪放在您的问题中。该消息与正则表达式无关。该消息的直接原因是使用关键字
53进行字典查找,可能位于texts['title'][i]:换句话说,texts['title']是一个字典,它不包含关键字53。没有更多的内容,很难说更多。 -
值
i是使用len(texts)而不是len(texts['title'])创建的。如果texts是一个列表,那么最好使用for item in texts: ... item ...。但它似乎是字典,然后最好不要将它与len()一起使用,因为它会产生错误的结果。所以主要问题是:texts