字体文件获取地址
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))
#遍历words中所有词语,单个词语不计算在内
counts = {}
for word in words:
if len(word) == 1:
continue
else:
counts[word] = counts.get(word,0)+1 #统计次数
#合并列表的元素,成为一个句子
words_1 = ''.join(counts.keys())
#词云图的可视化
#添加字体文件,配置对象参数,文本字体路径,宽度1000,高度700,背景白色
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="") #newlines=''可保证存储存的数据不空行
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
'''