import jieba
import wordcloud
f = open("D:\\pythonfiles\\告白气球.txt","r",encoding="utf-8")
t = f.read()
f.close()
ls = jieba.lcut(t)
txt = " ".join(ls)
w = wordcloud.WordCloud(width=1000,font_path="msyh.ttc",height=700)
w.generate(txt)
w.to_file("D:\\pythonfiles\\pywordcloud.png")
如上,执行后报错
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 0: invalid continuation byte
把文件另存一次,下面的编码选择UTF-8
保存后,继续执行,还是报错,如下:
OSError: cannot open resource
经查询是代码块font_path="msyh.ttc"有问题导致
这个变量取值的是字体,msyh表示微软雅黑,直接定位的字体目录C:\Windows\Fonts,找到微软雅黑字体