python wordcloud&jieba生成中文词云

python wordcloud&jieba生成中文词云

  • 代码
  • 效果图

代码

import wordcloud
import jieba

f = open('/Users/xx/Desktop/平凡的世界.txt','r')
words = []
text = f.read()
seg_list = jieba.cut(text, cut_all=True)
for word in seg_list:
    if len(word) == 1:
        continue
    else:
        words.append(word)
result_txt = ' '.join(words) 
# 中文需要指定字体,不然显示乱码
w = wordcloud.WordCloud(width=1000,height=1000,font_path='/System/Library/Fonts/PingFang.ttc')
w.generate(result_txt)
w.to_file('/Users/xx/Desktop/平凡的世界.png')

效果图

python wordcloud&jieba生成中文词云_第1张图片

你可能感兴趣的:(python,python,中文分词)