繁体   English   中英

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

[英]AttributeError: '_io.TextIOWrapper' object has no attribute 'lower'

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

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

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

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

text_files = ['file1.txt', 'file2.txt']

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

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'

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

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

尝试改变

documents = [open(f) for f in text_files]

documents = [open(f).read() for f in text_files]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM