利用jieba库和wordcloud库,进行中文词频统计并利用词云图进行数据可视化
安装jieba库
- 打开cmd输入pip install jieba
- 验证:python -m pip list
安装wordcloud
- 打开cmd输入pip install wheel
- 查看python版本:python
- wordcloud下载地址
- 选择对应的版本,例如:3.9.7版本的python,选择带39数字的
- 打开cmd切换到wordcloud文件的位置
- 输入pip install 路径
打开pycharm
- 创建项目
- pycharm右下角python
- 点击解释器设置(倒数第二个)
- 点击小齿轮,选择全部显示(第二个)
- 点击+号
- 选择系统解释器,定位到安装的python
- 查看有没有jieba库和wordcloud库
- 有就确定,开始编写代码
编写代码
字体文件获取地址
import jieba
import wordcloud
import re
import csv
text = open("E:\\python\\text1.txt","r",encoding="utf_8_sig").read()
words = jieba.lcut(re.sub(r'[,:。、?“;”()\n 0-9]',"",text))
counts = {}
for word in words:
if len(word) == 1:
continue
else:
counts[word] = counts.get(word,0)+1
words_1 = ''.join(counts.keys())
w = wordcloud.WordCloud(font_path="E:\\font\\simsun.ttf",width=1000,height=700,background_color="white")
w.generate(" ".join(jieba.lcut(words_1)))
w.to_file("图片路径.png")
counts = sorted(counts.items(),key = lambda x:x[1],reverse = True)
f = open('E:\\python\\01.csv','w',newline="")
writer = csv.writer(f)
for count in counts:
writer.writerow(count)
f.close()
'''
参考
jieba
https://blog.csdn.net/jinsefm/article/details/80645588
https://blog.csdn.net/codejas/article/details/80356544
wordcloud
https://mathpretty.com/10951.html
https://blog.csdn.net/kun1280437633/article/details/89474284
re
https://www.runoob.com/python/python-reg-expressions.html
csv
https://www.cnblogs.com/qican/p/11122206.html
https://www.jianshu.com/p/e6768d9af085
'''
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· 单线程的Redis速度为什么快?
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码