前言:受我表哥邀请,帮忙统计历年的高考英语词频,并保存到表格中,这样方便发给学生学习需要重点注意的单词,正好他也对python感兴趣,我就简单的给他写了一个小程序,每次把要统计的word文档放入data文件中就双击写好的程序可以给到Excel的单词的词频,也非常的方便对于不熟悉编程的人士运用它!
操作环境: window10, python37, jupyter
数据样式:
1、读取文件夹中的每个word名称
import os
filelist=os.listdir("./data")
for fileName in filelist:
pathName = "./data/" + fileName
print (pathName)
./data/2017全国卷1高考英语试题及其参考答案.docx
./data/2017年全国Ⅱ卷英语高考试题(Word精校版,含答案).docx
./data/2017年高考英语全国卷3(含答案).docx
./data/2018全国Ⅰ卷英语高考真题.docx
./data/2018全国Ⅱ卷英语高考真题.docx
./data/2018全国Ⅲ卷英语高考真题.docx
./data/2019年全国卷Ⅱ英语高考试题文档版.docx
./data/2019年全国卷Ⅲ高考英语试题.docx
./data/2019高考英语全国1卷.docx
2、读取每个word的文字
import docx2txt
my_text = docx2txt.process(pathName)
print (my_text)
为了避免干扰因素,我们可以提前去掉/n, /t, \xa0, 数字,A., B.,C., D.
, 去掉选项序号加上点是为了识别出它是选项,不是大写的字母。
3、数据清洗
这里进行数据清洗的目的主要是去掉中文,换行符,制表符,ABDC选项,并把单词大写换为小写等等
filelist=os.listdir("./data")
word_text = ""
for fileName in filelist:
my_text = docx2txt.process("./data/" + fileName)#读取word的内容
text = "".join(i for i in my_text if ord(i) < 256)#去除中文
word_text = word_text + text #把所有的试卷内容合并到一起
print ("导入:",fileName, " 字符长度:",len(text))
#替换掉干扰元素为空格
word_text = word_text.replace('\n',' ').replace('\xa0', ' ').replace('\t', ' ').replace('A.', ' ').replace('B.', ' ').replace('C.', ' ').replace('D.', ' ')
word_text = "".join(re.findall("[^\d']+",word_text)).lower()#替换掉数值,并把大写字母全部转为小写
附:字母大小转换
strs = "yangyou.Ayouleyang"
print(strs.upper()) # 把所有字符中的小写字母转换成大写字母
print(strs.lower()) # 把所有字符中的大写字母转换成小写字母
print(strs.capitalize()) # 把第一个字母转化为大写字母,其余小写
print(strs.title()) # 把每个单词的第一个字母转化为大写,其余小写
YANGYOU.AYOULEYANG
yangyou.ayouleyang
Yangyou.ayouleyang
Yangyou.Ayouleyang
4、词频统计
words = jieba.lcut(word_text) #分词
counts = {}
#过滤掉字符为1的词,包括符号
for word in words:
if len(word) == 1:
continue
else:
#把结果添加进字典,并统计词频
counts[word] = counts.get(word, 0) + 1
items = sorted(counts.items(), key=lambda item:item[1], reverse=True)#对字典进行排序
#把字典变为数组,方便写入Excel
all_page = []
for i in range(len(items)):
word, count = items[i]
page = [word, count]#生成数组
all_page.append(page)
5、写入表格
book = xlwt.Workbook(encoding = 'utf-8')#创建工作簿
sheet = book.add_sheet('高考',cell_overwrite_ok=True)#创建表名,cell_overwrite_ok=True用于确认同一个cell单元是否可以重设值
head = ['单词','词频']#定义表头,即Excel中第一行标题
for h in range(len(head)):
sheet.write(0,h,head[h])#写入表头
j = 1#第一行开始
for list in all_page:
k = 0
for date in list:
sheet.write(j, k, date)#迭代列,并写入数据,重新设置,需要cell_overwrite_ok=True
k = k+1
j = j+1
book.save('./高考单词词频统计表.xls')
6、源码汇总
import os,docx2txt, re, jieba, xlwt
filelist=os.listdir("./data")
word_text = ""
for fileName in filelist:
my_text = docx2txt.process("./data/" + fileName)#读取word的内容
text = "".join(i for i in my_text if ord(i) < 256)#去除中文
word_text = word_text + text #把所有的试卷内容合并到一起
print ("导入:",fileName, " 字符长度:",len(text))
#替换掉干扰元素为空格
word_text = word_text.replace('\n',' ').replace('\xa0', ' ').replace('\t', ' ').replace('A.', ' ').replace('B.', ' ').replace('C.', ' ').replace('D.', ' ')
word_text = "".join(re.findall("[^\d']+",word_text)).lower()#替换掉数值,并把大写字母全部转为小写
words = jieba.lcut(word_text) #分词
counts = {}
#过滤掉字符为1的词,包括符号
for word in words:
if len(word) == 1:
continue
else:
#把结果添加进字典,并统计词频
counts[word] = counts.get(word, 0) + 1
items = sorted(counts.items(), key=lambda item:item[1], reverse=True)#对字典进行排序
#把字典变为数组,方便写入Excel
all_page = []
for i in range(len(items)):
word, count = items[i]
page = [word, count]#生成数组
all_page.append(page)
book = xlwt.Workbook(encoding = 'utf-8')#创建工作簿
sheet = book.add_sheet('高考',cell_overwrite_ok=True)#创建表名,cell_overwrite_ok=True用于确认同一个cell单元是否可以重设值
head = ['单词','词频']#定义表头,即Excel中第一行标题
for h in range(len(head)):
sheet.write(0,h,head[h])#写入表头
j = 1#第一行开始
for list in all_page:
k = 0
for date in list:
sheet.write(j, k, date)#迭代列,并写入数据,重新设置,需要cell_overwrite_ok=True
k = k+1
j = j+1
book.save('./高考单词词频统计表.xls')
统计结果:
最后,把代码和文件夹的数据放在一起,只需要把要统计的word文档放进data中,双击count.py就完成统计,并生成“高考单词词频统计表.xls”