|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
1、pip install wordcloud 已经显示安装成功了
2、代码如下所示、txt文件存储是 utf-8
3、第二次在cmd中运行 pip install wordcloud 没有报错
txt文件连接如下:
https://python123.io/resources/pye/新时代中国特色社会主义.txt
- import jieba
- import wordcloud
- f = open('新时代中国特色社会主义.txt', 'r', encoding='utf-8')
- t = f.read()
- f.close()
- ls = jieba.lcut(t)
- txt = ' '.join(ls)
- w = wordcloud.WordCloud(font_path = 'msyh.ttc',width = 1000,\
- height = 700, background_color = 'white')
- w.generate(txt)
- w.to_file('grwordcloud.png')
复制代码
错误显示如下:
- Building prefix dict from the default dictionary ...
- Loading model from cache C:\Users\WANGWE~1\AppData\Local\Temp\jieba.cache
- Loading model cost 0.985 seconds.
- Prefix dict has been built successfully.
- Traceback (most recent call last):
- File "F:/360MoveData/Users/wangwenxiong/Desktop/词云试验.py", line 10, in <module>
- w.generate(txt)
- File "C:\Users\wangwenxiong\AppData\Local\Programs\Python\Python37-32\lib\site-packages\wordcloud\wordcloud.py", line 619, in generate
- return self.generate_from_text(text)
- File "C:\Users\wangwenxiong\AppData\Local\Programs\Python\Python37-32\lib\site-packages\wordcloud\wordcloud.py", line 601, in generate_from_text
- self.generate_from_frequencies(words)
- File "C:\Users\wangwenxiong\AppData\Local\Programs\Python\Python37-32\lib\site-packages\wordcloud\wordcloud.py", line 434, in generate_from_frequencies
- max_font_size=self.height)
- File "C:\Users\wangwenxiong\AppData\Local\Programs\Python\Python37-32\lib\site-packages\wordcloud\wordcloud.py", line 483, in generate_from_frequencies
- font = ImageFont.truetype(self.font_path, font_size)
- File "C:\Users\wangwenxiong\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PIL\ImageFont.py", line 642, in truetype
- return freetype(font)
- File "C:\Users\wangwenxiong\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PIL\ImageFont.py", line 639, in freetype
- return FreeTypeFont(font, size, index, encoding, layout_engine)
- File "C:\Users\wangwenxiong\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PIL\ImageFont.py", line 188, in __init__
- font, size, index, encoding, layout_engine=layout_engine
- OSError: cannot open resource
复制代码
|
|