如何修复错误'int'对象在尝试制作单词云图像时没有属性'text'[关闭]

问题描述 投票:-4回答:1

如何修复此错误,我正在尝试合并文本并创建一个文字云图像。

from os import path 
from PIL import Image
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator

text = [] 
cb = cbData #text dataset
tc = 0
for t in cb.text:
    text.append(t)
    tc += 1

all_text = " ".join(t for t in text)
print("Total words in all posts: ", len(all_text))

这是错误:

----> 3 for t in cb.text:
AttributeError: 'int' object has no attribute 'text'
python word-cloud
1个回答
0
投票

该错误只是告诉您正在尝试使用不存在的属性或方法。我建议列出确实存在的属性/方法,并找出从那里做的事情。这可以这样做:

# code that generates variable "cb" (not supplied in post)
print(dir(cb))

现在,查看输出并查看您应该尝试访问的方法/属性。也许它就像.Text.text()一样简单

注意:生成的列表不会告诉你它是否应该是.text.text()(差异是括号)所以如果一个不起作用只是使用另一个!

© www.soinside.com 2019 - 2024. All rights reserved.