python wordcloud库使用过程中的问题2
错误背景:
对txt生成词云;
错误代码:
#wcloud3.py import jieba import wordcloud txt="程序设计语言师计算机能够理解和识别用户操作意图的一种交互体系,它按照特定\ 规则组织计算机指令,使计算机能够自动进行各种运算处理。" w=wordcloud.WordCloud(width=1000,font_path="msyh.tcc",height=700) w.generate(" ".join(jieba.lcut(txt))) w.to_file("pywcloud.png")
错误提示:
Building prefix dict from the default dictionary ... Dumping model to file cache C:\Users\26408\AppData\Local\Temp\jieba.cache Loading model cost 3.340 seconds. Prefix dict has been built succesfully. Traceback (most recent call last): File "E:/1/python/file/wcloud3/wcloud3.py", line 7, in <module> w.generate(" ".join(jieba.lcut(txt))) File "C:\Users\26408\AppData\Local\Programs\Python\Python37\lib\site-packages\wordcloud\wordcloud.py", line 605, in generate return self.generate_from_text(text) File "C:\Users\26408\AppData\Local\Programs\Python\Python37\lib\site-packages\wordcloud\wordcloud.py", line 587, in generate_from_text self.generate_from_frequencies(words) File "C:\Users\26408\AppData\Local\Programs\Python\Python37\lib\site-packages\wordcloud\wordcloud.py", line 426, in generate_from_frequencies max_font_size=self.height) File "C:\Users\26408\AppData\Local\Programs\Python\Python37\lib\site-packages\wordcloud\wordcloud.py", line 473, in generate_from_frequencies font = ImageFont.truetype(self.font_path, font_size) File "C:\Users\26408\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageFont.py", line 275, in truetype return FreeTypeFont(font, size, index, encoding, layout_engine) File "C:\Users\26408\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageFont.py", line 144, in __init__ self.font = core.getfont(font, size, index, encoding, layout_engine=layout_engine) OSError: cannot open resource
cannot open resource;
查找问题:没有对应字体的文件;
解决方案:网上下载微软雅黑MSYH.ttf
Building prefix dict from the default dictionary ... Loading model from cache C:\Users\26408\AppData\Local\Temp\jieba.cache Loading model cost 2.064 seconds. Prefix dict has been built succesfully. >>>