AttributeError:'_ io.TextIOWrapper'对象没有属性'lower'

TJ1*_*TJ1 6 python attributeerror scikit-learn

我试图运行堆栈溢出是提供了一个示例在这里.

我在这里再次复制了代码:

from sklearn.feature_extraction.text import TfidfVectorizer
text_files = ['file1.txt', 'file2.txt']
documents = [open(f) for f in text_files]
tfidf = TfidfVectorizer().fit_transform(documents)
# no need to normalize, since Vectorizer will return normalized tf-idf
pairwise_similarity = tfidf * tfidf.T
Run Code Online (Sandbox Code Playgroud)

我添加的唯一内容就是这一行:

text_files = ['file1.txt', 'file2.txt']
Run Code Online (Sandbox Code Playgroud)

当我运行代码时,我收到此错误:

File "C:\Python33\lib\site-packages\sklearn\feature_extraction\text.py", line 195, in <lambda>
return lambda x: strip_accents(x.lower())
AttributeError: '_io.TextIOWrapper' object has no attribute 'lower'
Run Code Online (Sandbox Code Playgroud)

file1.txtfile2.txt输入的文本文件.我使用了错误的格式text_files吗?这个错误的原因是什么,我该如何解决?我真的很感激任何帮助.

Fel*_*Yan 10

open(f)是一个_io.TextIOWrapper对象,这就是它失败的原因.

尝试改变

documents = [open(f) for f in text_files]
Run Code Online (Sandbox Code Playgroud)

documents = [open(f).read() for f in text_files]
Run Code Online (Sandbox Code Playgroud)