import jieba import numpy from PIL import Image import csv from wordcloud import WordCloud with open('liur.csv', 'r', encoding='utf-8') as f: reader = csv.reader(f) data =[] for row in reader: try: data.append(row[1]) except IndexError: pass text = ''.join(row) words = jieba.lcut(text) print(words) bgImg=numpy.array(Image.open("nanhai.jpg")) cloud=WordCloud( font_path="C:\Windows\Fonts\simsun.ttc", background_color="white", mask=bgImg ).generate(text) cloud.to_file("词云.png") print("词云绘制成功")
时间: 2024-03-01 18:50:53 浏览: 121
在你的代码中,使用了 `row` 变量来拼接文本,这个变量应该写在 `for` 循环的内部,以保证能够正确拼接文本。同时,在使用 `WordCloud` 类生成词云时,也需要将变量名 `text` 传递给 `generate()` 方法,以保证能够正确生成词频。下面是修改后的代码:
```python
import jieba
import numpy
from PIL import Image
import csv
from wordcloud import WordCloud
with open('liur.csv', 'r', encoding='utf-8') as f:
reader = csv.reader(f)
data = []
for row in reader:
try:
data.append(row[1])
except IndexError:
pass
text = ''.join(data)
words = jieba.lcut(text)
print(words)
bgImg = numpy.array(Image.open("nanhai.jpg"))
cloud = WordCloud(
font_path="C:\Windows\Fonts\simsun.ttc",
background_color="white",
mask=bgImg
).generate(text)
cloud.to_file("词云.png")
print("词云绘制成功")
```
这样,你应该能够成功生成词云了。
阅读全文