当前位置:网站首页>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实际体验没多大区别。
边栏推荐
- By solving these three problems, the operation and maintenance efficiency will exceed 90% of the hospital
- el-autocomplete使用
- ROS2下使用ROS1 bag的方法
- Wireshark packet capture and common filtering methods
- scikit-image图像处理笔记
- Browser Storage for H5
- Insight into the general trend of the Internet, after reading this article, you will have a thorough understanding of Chinese domain names
- DevOps流程demo(实操记录)
- System basics - study notes (some command records)
- The use of three parameters of ref, out, and Params in Unity3D
猜你喜欢

Autoware--Beike Tianhui rfans lidar uses the camera & lidar joint calibration file to verify the fusion effect of point cloud images

VRRP overview and experiment

The cocos interview answers you are looking for are all here!

disabledDate 日期选择器 datePicker

初识网页与浏览器

【FAQ】CCAPI兼容EOS相机列表(2022年8月 更新)

邮件管理 过滤邮件

cs231n学习记录

The hook of the operation of the selenium module

DisabledDate date picker datePicker
随机推荐
Successful indie developers deal with failure & imposters
Transformer interprets and predicts instance records in detail
[issue resolved] - jenkins pipeline checkout timeout
大小屏适配
Problems encountered in installing Yolo3 target detection module in Autoware
LeetCode练习及自己理解记录(1)
H5 的浏览器存储
Mina的长连接和短连接
【考研结束第一天,过于空虚,想对自己进行总结一下】
Passing parameters in multiple threads
[问题已处理]-虚拟机报错contains a file system with errors check forced
设置文本向两边居中展示
指针常量与常量指针 巧记
What is Alibaba Cloud Express Beauty Station?
flink cdc 目前支持Gauss数据库源吗
格式化代码缩进的小技巧
多线程之传递参数
Into the pre-service, thought they play so flowers
LeetCode中常用语言的一些基本方法记录
Wireshark packet capture and common filtering methods