第一步;首先打开cmd输入:
pip install jieba
pip install wordcloud
然后在c盘创建一个文本命名为“国歌”
代码:
第一种方法:
from wordcloud import WordCloud
import jieba
f = open("C:\\国歌.txt","r")
txt = f.read()
f.close()
words=jieba.lcut(txt)
newtxt=' '.join(words)
wordcloud=WordCloud(font_path="msyh.ttc",\
background_color="white").generate(newtxt)
wordcloud.to_file("c:\\song.png")
第二种方法:
from wordcloud import WordCloud
import jieba
txt="起来
不愿做奴隶的人们
把我们的血肉
筑成我们新的长城
中华民族到了
最危险的时候
每个人被迫着
发出最后的吼声"
words=jieba.lcut(txt)
newtxt=' '.join(words)
wordcloud=WordCloud(font_path="msyh.ttc",\
background_color="white").generate(newtxt)
wordcloud.to_file("c:\\song.png")