Python 可迭代原始文本文档,收到字符串对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49806790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:15:19  来源:igfitidea点击:

Iterable over raw text documents expected, string object received

pythonstringiterator

提问by thegreatcoder

I am currently trying to build a naive Bayes classifier as mentioned in this link.Referring to the line

我目前正在尝试构建一个本链接中提到的朴素贝叶斯分类器参考线

X_new_tfidf = tfidf_transformer.transform(X_new_counts)

under the Training the Classifiersubheading, I had a similar line, X_new_counts = count_vect.transform(input.plot_movie)in my code which should take an iterable as an input to the transform function. The inputis a record from a DataFrame and is of type pd.Seriesand contains the following entries, out of which I send input.plot_movieas the input to the transform function:

Training the Classifier副标题下,X_new_counts = count_vect.transform(input.plot_movie)我的代码中有一条类似的行,它应该将一个可迭代对象作为转换函数的输入。这 input是来自 DataFrame 的记录,属于类型pd.Series并包含以下条目,我将其中的条目input.plot_movie作为输入发送到转换函数:

enter image description here

在此处输入图片说明

However, I get the following error: Iterable over raw text documents expected, string object received

但是,我收到以下错误: Iterable over raw text documents expected, string object received

How do I fix this error? I also referred to thisanswer where the person says that sis an iterable because it was assigned a string. I also came across this linkwhere a TypeError: 'String' object is not iterableis encountered. Am I missing something here? The links seem to contradict each other.

我该如何解决这个错误?我还提到了这个答案,其中有人说这s是一个可迭代的,因为它被分配了一个字符串。我还遇到了遇到a 的链接TypeError: 'String' object is not iterable。我在这里错过了什么吗?这些链接似乎相互矛盾。

EDIT:I just realized that input.plot_movieis of type unicode and decided to convert it to a string. I encounter the same error again.

编辑:我刚刚意识到这input.plot_movie是 unicode 类型并决定将其转换为字符串。我再次遇到同样的错误。

回答by thegreatcoder

The solution to this problem is because input is just a String, but what is needed is a list (or an iterable) containing a single element (which is nothing but the String itself).

这个问题的解决方案是因为输入只是一个字符串,但需要的是一个包含单个元素(它只是字符串本身)的列表(或一个可迭代对象)。

The error can be removed by adding the following line:

可以通过添加以下行来消除错误:

input=[input]

before

X_new_counts = count_vect.transform(input.plot_movie)