1.读文件,通过正则匹配

 1 def statisticWord():
 2     line_number = 0
 3     words_dict = {}
 4     with open (r'D:\test\test.txt',encoding='utf-8') as a_file:
 5         for line in a_file:
 6             words = re.findall(r'&#\d+;|&#\d+;|&\w+;',line)
 7             for word in words:
 8                 words_dict[word] = words_dict.get(word,0) + 1 #get the value of word, default is 0
 9         sort_words_dict = OrderedDict(sorted(words_dict.items(),key = lambda x : x[1], reverse = True))
10 #        sort_words_dict = sorted(words_dict, key = operator.itemgetter(1))
11         with open(r'D:\test\output.txt',encoding = 'utf-8', mode='w') as b_file:
12             for k,v in sort_words_dict.items():
13                 b_file.write("%-15s:%15s" % (k,v))
14                 b_file.write('\n')

2. 通过命令行参数

def statisticWord2():if len(sys.argv) == 1 or sys.argv[1] in {"-h", "--help"}:print("usage: filename_1 filename_2 ... filename_n")sys.exit()else:words = {}strip = string.whitespace + string.punctuation + string.digits + "\"'"for filename in sys.argv[1:]:for line in open(filename):for word in line.split():word = word.strip(strip) # remove all the combination of strip in prefix or suffixif len(word) >= 2:words[word] = words.get(word, 0) + 1for word in sorted(words):print("'{0}' occurs {1} times".format(word,words[word]))

转载于:https://www.cnblogs.com/zyf7630/p/3209976.html

Python 统计文本中单词的个数相关推荐

  1. c语言统计输入文本不同字母单词数,统计文本中单词的个数

    ㈠ 统计一行文本的单词个数:输入一行字符,统计其中单词的个数.个单词之间用空格分隔,空格数可以是多个, 代码部分: #include int main() { int count=0; char te ...

  2. python单词个数统计_Python 统计文本中单词的个数

    1.读文件,通过正则匹配 def statisticWord(): line_number = 0 words_dict = {} with open (r'D:\test\test.txt',enc ...

  3. python统计文本中单词出现次数

    任一个英文的纯文本文件,统计其中的单词出现的个数,其实就是考察re的运用,代码: #-*-coding:utf-8-*- import redef count_words(file_path):wit ...

  4. python统计有几个单词_统计文件中单词的个数---Shell及python版

    最近在看shell中有个题目为统计单词的个数,使用了awk功能,代码如下 #!/bin/bash if [ $# -ne ];then echo "Usage:basename $0 fil ...

  5. python中英文字频率_python实现统计文本中单词出现的频率详解

    本文实例为大家分享了python统计文本中单词出现频率的具体代码,供大家参考,具体内容如下 #coding=utf-8 import os from collections import Counte ...

  6. 用python统计字母个数_python统计字母个数 python 统计文本中字母个数

    python 统计文本中字母个数 作业文件等有人回答小编再发,之前发文件被屏蔽了明明转身就有一个温馨的港湾,却偏偏还要去追逐那无望的孤船. l = 0with open('file.txt', 'r' ...

  7. python统计句子中单词个数_【python统计单词数量】作文写作问答 - 归教作文网

    如何用python统计一个txt文件中某个单词出现的次数 1.首先,定义一个变量,保存要统计的英文文章. 2.接着,定义两个数组,保存文章中的单词,以及各单词的词频. 3.从文章中分割出所有的单词,保 ...

  8. python统计单词出现次数最多的5个单词_【Python】统计文本中单词的出现次数前十的单词...

    代码: # 读取一个文本,并且统计文本中单词的出现次数 def read_file(): # 在windows环境中的编码问题,指定utf-8 with open('F:/python源码/实验区/0 ...

  9. [c]输入一个英文句子,统计句子中单词的个数

    #include <stdio.h> #include <stdlib.h> //输入一个英文句子,统计句子中单词的个数 void main() { char s[256],p ...

最新文章

  1. 基于pytorch的模型剪枝+模型量化+BN合并+TRT部署(cifar数据)(2)
  2. 9.逆向-函数调用约定
  3. 【鸿蒙 HarmonyOS】UI 组件 ( 进度条 ProgressBar 和 RoundProgressBar 组件 )
  4. 汇编语言--loop指令
  5. 【OS】Linux命令如何放到后台运行
  6. one-to-many many-to-one 为什么只生成了一张表呢?
  7. 游戏设计中的算法题——计算宝物升级所需的资源数
  8. Android Fragment功能的例子
  9. 设置网页打开默认全屏_微信公众号里的视频不能进行全屏播放的解决方法
  10. Feature Layer with selection(ArcGIS JS Api 图上点选)
  11. [csu/coj 1632]LCP
  12. [bzoj1027][JSOI2007]合金
  13. 主题:基于非合作博弈模型多微网交易策略研究 参考文档:《基于博弈论的多微电网系统交易模式研究》完全复现
  14. ascll码和常用5000汉字(做字库可能会用到)
  15. perl:DNA序列翻译成氨基酸序列的若干方法,直接法,简并法,哈希法,以及perl中的uc和lc函数(上)
  16. Java宠物商店源代码
  17. 七个问题透视百度智慧商业平台
  18. nfcwriter下载 ios_iOS、iPadOS、macOS开启公测
  19. bind: An operation on a socket could not be performed because the system lacked sufficient buffer sp
  20. GIC 基础知识介绍 (一)

热门文章

  1. 关于Domain-Specific Languages
  2. eclipse中hadoop2.3.0环境部署及在eclipse中直接提交mapreduce任务
  3. hdu5108枚举因子求最小的m
  4. 【C 语言】二级指针作为输入 ( 自定义二级指针内存 | 二级指针 排序 | 通过 交换指针方式 进行排序 )
  5. 【Google Play】App Bundle 使用详解 ( 简介 | 应用内更新 | 即时更新 | 灵活更新 )
  6. 【Flutter】Flutter 手势交互 ( 点击事件处理 | 点击 onTap | 双击 | 长按 onLongPress | 点击取消 | 按下 onTapDown | 抬起 onTapUp )
  7. 【计算理论】计算理论总结 ( 正则表达式转为非确定性有限自动机 NFA | 示例 ) ★★
  8. 【Android 性能优化】应用启动优化 ( 安卓应用启动分析 | Launcher 应用启用普通安卓应用 | 应用进程分析 )
  9. 定时器 线程池\进程池
  10. 微信小程序打开PDF