当前位置:网站首页>txt文件英语单词词频统计
txt文件英语单词词频统计
2022-08-05 05:25:00 【回首思】
目录
一、需求分析
把txt文件里的英语单词按照出现次数排序并生成csv文件,如果次数相同按照单词的md5值来排序
二、相关库列表
- pandas
- re
- collections
- hashlib
三、代码在此
- 打开文件
txt_file = open(file_path, 'r') - 读取文件内容
txt_data = txt_file.read() - 字母全小写
txt_lower = txt_data.lower() - 正则表达式去特殊符号
# 正则表达式去除特殊符号 punc = '~`!#$%^&*()_+-=|\';":/.,?><~·!@#¥%……&*()——+-=“:’;、。,?》《{}\n' # 调用正则表达 txt_query = re.sub(r"[%s]+" % punc, "", txt_lower) - 调用统计库对词频统计
# 使用空格来对字符串进行裁切 txt_list = txt_query.split(' ') # 调用统计库来对词频进行统计 word = Counter(txt_list) - 把每个单词的信息写入到一个列表
# 创建一个列表来接收DataFrame原型 pa_list = [] # 把键、值和根据键名生成的md5写入原型里 for key, value in word.items(): pa_list.append([key, value, hashlib.md5(key.encode('utf-8')).hexdigest()]) - 调用pandas完成排序和文件导出
# 生成DataFrame pd_data = pd.DataFrame(pa_list) # 根据值和md5进行排序 dataexclex = pd_data.sort_values([1, 2]) # 把文件导出 dataexclex.to_csv(f'./{new_file_name}.csv') - 完整代码
import pandas as pd import re from collections import Counter import hashlib def Word_frequency_statistics(file_path,new_file_name): # 打开文件 txt_file = open(file_path, 'r') # 读取文件内容 txt_data = txt_file.read() # 字母小写化 txt_lower = txt_data.lower() # 正则表达式去除特殊符号 punc = '~`!#$%^&*()_+-=|\';":/.,?><~·!@#¥%……&*()——+-=“:’;、。,?》《{}\n' # 调用正则表达 txt_query = re.sub(r"[%s]+" % punc, "", txt_lower) # 使用空格来对字符串进行裁切 txt_list = txt_query.split(' ') # 调用统计库来对词频进行统计 word = Counter(txt_list) # 创建一个列表来接收DataFrame原型 pa_list = [] # 把键、值和根据键名生成的md5写入原型里 for key, value in word.items(): pa_list.append([key, value, hashlib.md5(key.encode('utf-8')).hexdigest()]) # 生成DataFrame pd_data = pd.DataFrame(pa_list) # 根据值和md5进行排序 dataexclex = pd_data.sort_values([1, 2]) # 把文件导出 dataexclex.to_csv(f'./{new_file_name}.csv') # 要词频统计的文件路径 file_path = '' # 导出时的文件名称 new_file_name = '' # 调用函数统计词频 Word_frequency_statistics(file_path,new_file_name)
四、一些问题
我只试过wav文件,mp3文件一个也可以,前提是文件夹里的文件全是要参与的文件,安装库的时候遇到问题简易回退一个大版本0.9.0=>0.8.0实际体验没多大区别。
边栏推荐
猜你喜欢

多线程之传递参数

selenium模块的操作之拉钩
![[问题已处理]-虚拟机报错contains a file system with errors check forced](/img/07/1222a44dd52b359bf7873e6f3b7ebf.png)
[问题已处理]-虚拟机报错contains a file system with errors check forced

BIO,NIO,AIO实践学习笔记(便于理解理论)

Cocos Creator Mini Game Case "Stick Soldier"

selenium学习

el-progress实现进度条颜色不同

What are some things that you only know when you do operation and maintenance?

VRRP overview and experiment

Wireshark packet capture and common filtering methods
随机推荐
摆脱极域软件的限制
GetEnumerator method and MoveNext and Reset methods in Unity
VLAN is introduced with the experiment
The future of cloud gaming
link 和@improt的区别
BIO, NIO, AIO practical study notes (easy to understand theory)
D39_Eulerian Angles and Quaternions
浏览器存储WebStorage
Shadowless Cloud Desktop
ev加密视频转换成MP4格式,亲测可用
sql server 重复值在后面计数
网络协议基础-学习笔记
wc, grep, tar, vi/vim
DevOps process demo (practical record)
Cocos Creator Mini Game Case "Stick Soldier"
指针常量与常量指针 巧记
sql server duplicate values are counted after
错误记录集锦(遇到则记下)
cs231n学习记录
Growth: IT Operations Trends Report