Python 合并多个TXT文件并统计词频的实现

yipeiwu_com5年前Python基础

需求是:针对三篇英文文章进行分析,计算出现次数最多的 10 个单词

逻辑很清晰简单,不算难, 使用 python 读取多个 txt 文件,将文件的内容写入新的 txt 中,然后对新 txt 文件进行词频统计,得到最终结果。

代码如下:(在Windows 10,Python 3.7.4环境下运行通过)

# coding=utf-8

import re
import os

# 获取源文件夹的路径下的所有文件
sourceFileDir = 'D:\\Python\\txt\\'
filenames = os.listdir(sourceFileDir)

# 打开当前目录下的 result.txt 文件,如果没有则创建
# 文件也可以是其他类型的格式,如 result.js
file = open('D:\\Python\\result.txt', 'w')

# 遍历文件
for filename in filenames:
 filepath = sourceFileDir+'\\'+filename
 # 遍历单个文件,读取行数,写入内容
 for line in open(filepath):
  file.writelines(line)
  file.write('\n')

# 关闭文件
file.close()


# 获取单词函数定义
def getTxt():
 txt = open('result.txt').read()
 txt = txt.lower()
 txt = txt.replace(''', '\'')
 # !"@#$%^&*()+,-./:;<=>?@[\\]_`~{|}
 for ch in '!"'@#$%^&*()+,-/:;<=>?@[\\]_`~{|}':
  txt.replace(ch, ' ')
  return txt

# 1.获取单词
hamletTxt = getTxt()

# 2.切割为列表格式,'' 兼容符号错误情况,只保留英文单词
txtArr = re.findall('[a-z\''A-Z]+', hamletTxt)

# 3.去除所有遍历统计
counts = {}
for word in txtArr:
 # 去掉一些常见无价值词
 forbinArr = ['a.', 'the', 'a', 'i']
 if word not in forbinArr:
  counts[word] = counts.get(word, 0) + 1

# 4.转换格式,方便打印,将字典转换为列表,次数按从大到小排序
countsList = list(counts.items())
countsList.sort(key=lambda x: x[1], reverse=True)

# 5. 输出结果
for i in range(10):
 word, count = countsList[i]
 print('{0:<10}{1:>5}'.format(word, count))

效果如下图:

另一种更简单的统计词频的方法:

# coding=utf-8
from collections import Counter

# words 为读取到的结果 list
words = ['a', 'b' ,'a', 'c', 'v', '4', ',', 'w', 'y', 'y', 'u', 'y', 'r', 't', 'w']
wordCounter = Counter(words)
print(wordCounter.most_common(10))

# output: [('y', 3), ('a', 2), ('w', 2), ('b', 1), ('c', 1), ('v', 1), ('4', 1), (',', 1), ('u', 1), ('r', 1)]


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python3 selenium自动化 frame表单嵌套的切换方法

python3 selenium自动化 frame表单嵌套的切换方法

在web自动化测试中,测试工程师经常会碰到frame表单嵌套结构,直接定位会报错,我们需要切换表单后才能成功定位。 我拿QQ邮箱登录来作为例子说下frame怎么切换。 qq邮箱页面按F...

python安装requests库的实例代码

requests是python实现的简单易用的HTTP库,使用起来比urllib简洁很多 因为是第三方库,所以使用前需要cmd安装 pip install requests 安装完...

Python3 chardet模块查看编码格式的例子

Python3 chardet模块查看编码格式的例子

如下所示: 需要注意的是,如果遇到GBK2312等编码的,在decode和encode时,一律使用GBK进行编码或者解码,这是因为GBK是其他GBK编码的超集,向下兼容所有的GBK编码...

详解Python中heapq模块的用法

详解Python中heapq模块的用法

heapq 模块提供了堆算法。heapq是一种子节点和父节点排序的树形数据结构。这个模块提供heap[k] <= heap[2*k+1] and heap[k] <= hea...

Python查找第n个子串的技巧分享

Problem Python中str类自带的find、index方法可以返回第一个匹配的子串的位置,但是如果实际使用中需要查找第2个甚至第n个子串的位置该怎么办呢。也许有的码友可能会用到...