目录
1.项目简介:
编写程序统计QQ导出的聊天记录,对发言人发言的次数进行统计,并将出现次数最多(次数排名前10)的发言人进行降序显示,并绘制柱状图。
有简单界面显示
2.代码如下:
1.countwords.py
import matplotlib.pyplot as plt
import re
"""
函数名:ReadDir
功能:读取文件并存入列表中
参数:path 文件路径
返回值:DataList 列表存文件数据
"""
def ReadDir(path):
f = open(path, "r", encoding="utf-8") # 只读打开文件 utf-8 编码
DataList = [] # 列表
for item in f.readlines(): # 按行读取
if "2022-" in item and "Q" not in item and "系" not in item:#只存含有 2022 且不含有Q 系的行
lin = item.split()[-1].split('(')[0].split('<')[0] # 每行先按空格分两份 取最后一个 再按( 或<分两份 取第一段
DataList.append(lin) # 追加到列表
f.close()
print(DataList)
return DataList
"""
函数名:DataChuli
功能:处理读取到的数据 大到小排序 替换“”为匿名 替换ego 为高老师 选取前10位
参数:DataList 读取到的数据
返回值:无
"""
def DataChuli(DataList):
MyData = {} # 字典存名字和发言次数
for item in DataList: #修改列表中的 “”和 ego
if item == "":
DataList[DataList.index(item)] = "匿名"
if item == "ego":
DataList[DataList.index(item)] = "高老师"
for item in DataList:
# 从列表中读取一个名字加入字典中作为 键
# 用get从列表中获取数据加入字典 若字典中有则返回值并加一
MyData[item] = MyData.get(item, 0) + 1
NameList = list(MyData.items()) # 将字典放入列表中
SortNmae = sorted(NameList, key=lambda x: x[1], reverse=True) # 按值大小排序
print(SortNmae)
global name_key_list
name_key_list=[]#名字
global name_val_list
name_val_list =[]#列表
for i in range(0, 10):
name_key = SortNmae[i].__getitem__(0)
name_val = SortNmae[i].__getitem__(1)
name_key_list.append(name_key)
name_val_list.append(name_val)
print("名字;{} || 发言次数:{}".format(name_key, name_val))
"""
函数名:Baocun
功能:保存图表
参数:无
返回值:无
"""
def Baocun():
plt.savefig("柱状图.png")
"""
函数名:Xianshi
功能:柱形图表显示
参数:无
返回值:无
"""
def Xianshi():
plt.rcParams['font.family'] = ['Arial Unicode MS', 'Microsoft Yahei', 'SimHei', 'sans-serif'] #
plt.rcParams['font.size'] = 12
plt.figure(figsize=(30, 8), facecolor="#B0C4DE", dpi=80)
plt.barh(name_key_list,name_val_list, height=0.5, color=['b', 'r', 'g', 'y', 'c', 'm', 'y', 'k', 'c', 'g', 'b'])
plt.xlabel("发言次数")
plt.ylabel("姓名")
plt.title("21071班QQ群发言次数统计")
plt.show()
2.UIjiemian.py
import tkinter as tk
import tkinter.filedialog
from countwords import ReadDir,DataChuli,Xianshi,Baocun
"""
函数名:fun1 导入按钮的功能函数
功能:打开一个对话框 ,选择文件并返回路径字符串
参数:无
返回值:无
"""
def fun1():
path=tkinter.filedialog.askopenfilename()#打开一个对话框,选择文件并返回路径字符串
print(path)
DirPath.set(path)
global Glog_DirPath
Glog_DirPath=DirPath.get()
"""
函数名:fun2 打开按钮的功能函数[]
功能:调用 读数据 处理数据 图标显示 函数
参数:无
返回值:无
"""
def fun2():
Temp = ReadDir(Glog_DirPath)
DataChuli(Temp)
Xianshi()
"""
函数名:fun3 保存按钮的功能函数
"""
def fun3():
Baocun()
"""
函数名:JieMian
功能:设置导入文件的ui
参数:无
返回值:无
"""
def JieMian():
window = tk.Tk()
window.title("聊天消息词频统计")
window.geometry('400x300')
# window.iconbitmap("app.ico")#更改窗口图表
global DirPath
DirPath= tk.StringVar()
label = tk.Label(window, text="欢迎使用", bg="silver", \
font=("宋体", 20), width=30, height=5)
label.pack()
button_in = tk.Button(window, text="文件导入", bg="silver", \
font=("宋体", 10), width=10, height=2, command=fun1)
button_in.place(x=270, y=150)
in_put = tk.Entry(window, textvariable=DirPath)
in_put.place(x=120, y=150)
button_open = tk.Button(window, text="打开", bg="silver", \
font=("宋体", 10), width=10, height=2, command=fun2)
button_open.place(x=100, y=200)
button_open = tk.Button(window, text="保存", bg="silver", \
font=("宋体", 10), width=10, height=2, command=fun3)
button_open.place(x=200, y=200)
window.mainloop()
#调用
JieMian()
3.运行结果,界面显示:
![](https://img-blog.csdnimg.cn/9745b6fbad734f2cb228f2dedca59064.jpeg)
4.柱状图显示
![](https://img-blog.csdnimg.cn/75c53eca0a8e49139af05b0c49039613.jpeg)