Python统计纯文本文件中英文单词出现个数的方法总结【测试可用】

 更新时间:2018年07月25日 09:56:52   作者:wanlifeipeng  
这篇文章主要介绍了Python统计纯文本文件中英文单词出现个数的方法,结合实例形式总结分析了Python针对文本文件的读取,以及统计文本文件中英文单词个数的4种常用操作技巧,需要的朋友可以参考下

本文实例讲述了Python统计纯文本文件中英文单词出现个数的方法。分享给大家供大家参考,具体如下:

第一版: 效率低

# -*- coding:utf-8 -*-
#!python3
path = 'test.txt'
with open(path,encoding='utf-8',newline='') as f:
  word = []
  words_dict= {}
  for letter in f.read():
    if letter.isalnum():
      word.append(letter)
    elif letter.isspace(): #空白字符 空格 \t \n
      if word:
        word = ''.join(word).lower() #转小写
        if word not in words_dict:
          words_dict[word] = 1
        else:
          words_dict[word] += 1
        word = []
#处理最后一个单词
if word:
  word = ''.join(word).lower() # 转小写
  if word not in words_dict:
    words_dict[word] = 1
  else:
    words_dict[word] += 1
  word = []
for k,v in words_dict.items():
  print(k,v)

运行结果:

we 4
are 1
busy 1
all 1
day 1
like 1
swarms 1
of 6
flies 1
without 1
souls 1
noisy 1
restless 1
unable 1
to 1
hear 1
the 7
voices 1
soul 1
as 1
time 1
goes 1
by 1
childhood 1
away 2
grew 1
up 1
years 1
a 1
lot 1
memories 1
once 1
have 2
also 1
eroded 1
bottom 1
childish 1
innocence 1
regardless 1
shackles 1
mind 1
indulge 1
in 1
world 1
buckish 1
focus 1
on 1
beneficial 1
principle 1
lost 1
themselves 1

第二版:

缺点:遇到大文件要一次读入内存,性能不好

# -*- coding:utf-8 -*-
#!python3
import re
path = 'test.txt'
with open(path,'r',encoding='utf-8') as f:
  data = f.read()
  word_reg = re.compile(r'\w+')
  #word_reg = re.compile(r'\w+\b')
  word_list = word_reg.findall(data)
  word_list = [word.lower() for word in word_list] #转小写
  word_set = set(word_list) #避免重复查询
  # words_dict = {}
  # for word in word_set:
  #   words_dict[word] = word_list.count(word)
  # 简洁写法
  words_dict = {word: word_list.count(word) for word in word_set}
  for k,v in words_dict.items():
    print(k,v)

运行结果:

on 1
also 1
souls 1
focus 1
soul 1
time 1
noisy 1
grew 1
lot 1
childish 1
like 1
voices 1
indulge 1
swarms 1
buckish 1
restless 1
we 4
hear 1
childhood 1
as 1
world 1
themselves 1
are 1
bottom 1
memories 1
the 7
of 6
flies 1
without 1
have 2
day 1
busy 1
to 1
eroded 1
regardless 1
unable 1
innocence 1
up 1
a 1
in 1
mind 1
goes 1
by 1
lost 1
principle 1
once 1
away 2
years 1
beneficial 1
all 1
shackles 1

第三版:

# -*- coding:utf-8 -*-
#!python3
import re
path = 'test.txt'
with open(path, 'r', encoding='utf-8') as f:
  word_list = []
  word_reg = re.compile(r'\w+')
  for line in f:
    #line_words = word_reg.findall(line)
    #比上面的正则更加简单
    line_words = line.split()
    word_list.extend(line_words)
  word_set = set(word_list) # 避免重复查询
  words_dict = {word: word_list.count(word) for word in word_set}
  for k, v in words_dict.items():
    print(k, v)

运行结果:

childhood 1
innocence, 1
are 1
of 6
also 1
lost 1
We 1
regardless 1
noisy, 1
by, 1
on 1
themselves. 1
grew 1
lot 1
bottom 1
buckish, 1
time 1
childish 1
voices 1
once 1
restless, 1
shackles 1
world 1
eroded 1
As 1
all 1
day, 1
swarms 1
we 3
soul. 1
memories, 1
in 1
without 1
like 1
beneficial 1
up, 1
unable 1
away 1
flies 1
goes 1
a 1
have 2
away, 1
mind, 1
focus 1
principle, 1
hear 1
to 1
the 7
years 1
busy 1
souls, 1
indulge 1

第四版:使用Counter统计

# -*- coding:utf-8 -*-
#!python3
import collections
import re
path = 'test.txt'
with open(path, 'r', encoding='utf-8') as f:
  word_list = []
  word_reg = re.compile(r'\w+')
  for line in f:
    line_words = line.split()
    word_list.extend(line_words)
  words_dict = dict(collections.Counter(word_list)) #使用Counter统计
  for k, v in words_dict.items():
    print(k, v)

运行结果:

We 1
are 1
busy 1
all 1
day, 1
like 1
swarms 1
of 6
flies 1
without 1
souls, 1
noisy, 1
restless, 1
unable 1
to 1
hear 1
the 7
voices 1
soul. 1
As 1
time 1
goes 1
by, 1
childhood 1
away, 1
we 3
grew 1
up, 1
years 1
away 1
a 1
lot 1
memories, 1
once 1
have 2
also 1
eroded 1
bottom 1
childish 1
innocence, 1
regardless 1
shackles 1
mind, 1
indulge 1
in 1
world 1
buckish, 1
focus 1
on 1
beneficial 1
principle, 1
lost 1
themselves. 1

注:这里使用的测试文本test.txt如下:

We are busy all day, like swarms of flies without souls, noisy, restless, unable to hear the voices of the soul. As time goes by, childhood away, we grew up, years away a lot of memories, once have also eroded the bottom of the childish innocence, we regardless of the shackles of mind, indulge in the world buckish, focus on the beneficial principle, we have lost themselves.

PS:这里再为大家推荐2款相关统计工具供大家参考:

在线字数统计工具:
http://tools.jb51.net/code/zishutongji

在线字符统计与编辑工具:
http://tools.jb51.net/code/char_tongji

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python文件与目录操作技巧汇总》、《Python文本文件操作技巧汇总》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

相关文章

  • 解决Python出现_warn_unsafe_extraction问题的方法

    解决Python出现_warn_unsafe_extraction问题的方法

    这篇文章主要为大家详细介绍了解决Python出现'_warn_unsafe_extraction'问题的方法,感兴趣的小伙伴们可以参考一下
    2016-03-03
  • 删除pycharm鼠标右键快捷键打开项目的操作

    删除pycharm鼠标右键快捷键打开项目的操作

    这篇文章主要介绍了删除pycharm鼠标右键快捷键打开项目的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • Python中使用pypdf2合并、分割、加密pdf文件的代码详解

    Python中使用pypdf2合并、分割、加密pdf文件的代码详解

    这篇文章主要介绍了Python中使用pypdf2合并、分割、加密pdf文件的代码,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-05-05
  • Python OpenCV特征检测之特征匹配方式详解

    Python OpenCV特征检测之特征匹配方式详解

    OpenCV中提供了两种技术用于特征匹配,分别为Brute-Force匹配器和基于FLANN的匹配器。本文将为大家详细介绍一下这两种匹配方式,需要的可以参考一下
    2021-12-12
  • Python爬虫利用cookie实现模拟登陆实例详解

    Python爬虫利用cookie实现模拟登陆实例详解

    这篇文章主要介绍了Python爬虫利用cookie实现模拟登陆实例详解的相关资料,需要的朋友可以参考下
    2017-01-01
  • Python机器学习应用之支持向量机的分类预测篇

    Python机器学习应用之支持向量机的分类预测篇

    最近完成的一个项目用到了SVM,之前也一直有听说支持向量机,知道它是机器学习中一种非常厉害的算法。利用将近一个星期的时间学习了一下支持向量机,把原理推了一遍,感觉支持向量机确实挺厉害的,这篇文章带你了解它
    2022-01-01
  • Python读取xlsx文件的实现方法

    Python读取xlsx文件的实现方法

    这篇文章主要介绍了Python读取xlsx文件的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07
  • django框架ModelForm组件用法详解

    django框架ModelForm组件用法详解

    这篇文章主要介绍了django框架ModelForm组件用法,结合实例形式较为详细的分析了Django框架ModelForm组件相关功能、原理、使用方法及操作注意事项,需要的朋友可以参考下
    2019-12-12
  • Python设计模式之状态模式原理与用法详解

    Python设计模式之状态模式原理与用法详解

    这篇文章主要介绍了Python设计模式之状态模式原理与用法,简单描述了状态模式的概念、原理并结合实例形式分析了Python实现与使用状态模式的相关操作技巧,需要的朋友可以参考下
    2019-01-01
  • 使用python和pygame绘制繁花曲线的方法

    使用python和pygame绘制繁花曲线的方法

    本篇文章主要介绍了使用python和pygame绘制繁花曲线的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-02-02

最新评论