import jieba import numpy from PIL import Image import csv from wordcloud import WordCloud with open("douban.csv","r",encoding="utf-8") as file: data=csv.reader(file) for row in data: print(data) # 空字符串,装入所有评论 str="" # 循环读取数据[用户名,时间,地点,星级,内容] for i in data: # print(i[4]) str+=i[4] # print(str) # 切割词语 cutWord ="===".join(jieba.cut(str)) print(cutWord) # 读取图像模型 bgImg=numpy.array(Image.open("haixing.jpeg")) # 绘制词云 cloud=WordCloud( # 文字路径,本地文字路径 font_path="C:\Windows\Fonts\simsun.ttc", background_color="white", mask=bgImg ).generate(cutWord) cloud.to_file("词云.png") print("词云绘制成功")改成正确的代码
时间: 2024-04-28 14:19:27 浏览: 138
import jieba
import numpy
from PIL import Image
import csv
from wordcloud import WordCloud
try:
with open("douban.csv","r",encoding="utf-8") as file:
data=csv.reader(file)
for row in data:
print(data)
# 空字符串,装入所有评论
str=""
# 循环读取数据[用户名,时间,地点,星级,内容]
for i in data:
# print(i[4])
str+=i[4]
# print(str)
# 切割词语
cutWord ="===".join(jieba.cut(str))
print(cutWord)
# 读取图像模型
bgImg=numpy.array(Image.open("haixing.jpeg"))
# 绘制词云
cloud=WordCloud(
# 文字路径,本地文字路径
font_path="C:\Windows\Fonts\simsun.ttc",
background_color="white",
mask=bgImg
).generate(cutWord)
cloud.to_file("词云.png")
print("词云绘制成功")
except Exception as e:
print(e)
阅读全文