gpt4 book ai didi

python - AttributeError: '_io.TextIOWrapper' 对象没有属性 'lower'

转载 作者:太空狗 更新时间:2023-10-29 22:22:41 25 4
gpt4 key购买 nike

我正在尝试运行堆栈溢出中提供的示例 here .

我又把代码复制过来了:

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]

关于python - AttributeError: '_io.TextIOWrapper' 对象没有属性 'lower',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20831882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com