【问题标题】:KeyError: 53 when using re moduleKeyError: 53 使用 re 模块时
【发布时间】: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

标签: python nltk python-re


【解决方案1】:

问题是我正在处理一个 csv 文件并且我删除了一些行但我没有重置索引,因此当我们到达迭代 nb 53 即索引 53 时我们找不到它,因为它被删除了.

texts.reset_index(inplace = True, drop = True)

【讨论】:

    猜你喜欢
    • 2019-01-28
    • 1970-01-01
    • 2016-06-14
    • 2013-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    相关资源
    最近更新 更多