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

 def statisticWord():
line_number = 0
words_dict = {}
with open (r'D:\test\test.txt',encoding='utf-8') as a_file:
for line in a_file:
words = re.findall(r'&#\d+;|&#\d+;|&\w+;',line)
for word in words:
words_dict[word] = words_dict.get(word,0) + 1 #get the value of word, default is 0
sort_words_dict = OrderedDict(sorted(words_dict.items(),key = lambda x : x[1], reverse = True))
# sort_words_dict = sorted(words_dict, key = operator.itemgetter(1))
with open(r'D:\test\output.txt',encoding = 'utf-8', mode='w') as b_file:
for k,v in sort_words_dict.items():
b_file.write("%-15s:%15s" % (k,v))
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 suffix
if len(word) >= 2:
words[word] = words.get(word, 0) + 1
for word in sorted(words):
print("'{0}' occurs {1} times".format(word,words[word]))

Python 统计文本中单词的个数的更多相关文章

  1. python统计文本中每个单词出现的次数

    .python统计文本中每个单词出现的次数: #coding=utf-8 __author__ = 'zcg' import collections import os with open('abc. ...

  2. java统计文本中单词出现的个数

    package com.java_Test; import java.io.File; import java.util.HashMap; import java.util.Iterator; imp ...

  3. 统计文件中单词的个数---Shell及python版

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

  4. shell统计文本中单词的出现次数

    Ubuntu14.04 给定一个文本,统计其中单词出现的次数 方法1 # solution 1 grep与awk配合使用,写成一个sh脚本 fre.sh sh fre.sh wordfretest.t ...

  5. HDU_2030——统计文本中汉字的个数

    Problem Description 统计给定文本文件中汉字的个数.   Input 输入文件首先包含一个整数n,表示测试实例的个数,然后是n段文本.   Output 对于每一段文本,输出其中的汉 ...

  6. JAVA实验--统计文章中单词的个数并排序

    分析: 1)要统计单词的个数,就自己的对文章中单词出现的判断的理解来说是:当出现一个非字母的字符的时候,对前面的一部分字符串归结为单词 2)对于最后要判断字母出现的个数这个问题,我认为应该是要用到ma ...

  7. 使用xargs同步文本中单词出现个数

    #!/bin/bash # 分析一个文本文件中单词出现的频率. # 使用 'xargs' 将文本行分解为单词. # 检查命令行上输入的文件. ARGS= E_BADARGS= E_NOFILE= if ...

  8. C语言算法--统计字符串中单词的个数

    #include <stdio.h> #include <string.h> #include <stdlib.h> int main(void) { int le ...

  9. Python 基础 - 统计文本里单词的个数以及出现的次数

    # -*- coding:utf-8 -*- #author:V def tol (file1,gui): #写一个方法,定义文件,or 匹配规则 import re patt = re.compil ...

随机推荐

  1. 《Java编程那点事儿》读书笔记(四)——String和StringBuffer

    String 1.toString:显示对象内容时系统自动调用的方法. public class TOSTRING { public String toString(){ return "t ...

  2. 别在细节上栽跟头------------mysql 字段类型详解

    也许你平时不在意,在设计数据库的时候,数字就设成int(10) 字符串就设成varchar(20)或者text 普通情况下是没有问题的,但是若不理解字段类型和长度的含义,总有一天你会在这里栽跟头, 这 ...

  3. spring各个包之间的依赖关系

    从图中可以看到: 1.spring core,spring beans被其他较多包依赖,spring aop,spring context,spring expression分别被两个包依赖,而spr ...

  4. Objective-C 类的继承、方法的重写和重载

    一.类的继承 Objective-c中类的继承与C++类似,不同的是Objective-c不支持多重继承,一个类只能有一个父类,单继承使Objective-c的继承关系很简单,易于管理程序.Objec ...

  5. 网易云课堂学习之VS相关

    1.为开发好的项目文件瘦身 如:在项目文件ScreenCapture中,只需保留框起来的两个文件即可 而且在框起来的ScreenCapture里的Debug文件也可以删掉,整个文件由75.4 MB变为 ...

  6. visual studio 中删除多余的空白行

    替换  Ctrl+H  正则  勾选  替换 ^\s*\n 为空

  7. win7下搭建opengles2.0编程环境

    原帖地址:http://sixgod.org/archives/72   1.下载AMD的OpenGL ES2.0的模拟器,地址: http://www.opengles-book.com/ESEmu ...

  8. 函数lock_rec_add_to_queue

    在原来的type_mode基础上,加上LOCK_REC /*********************************************************************// ...

  9. Winform——计算器

    namespace 计算器2._0 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } pr ...

  10. CSS3之边框样式(动画过渡)

    简述 CSS3中transition属性定义了过渡,我们可以使用它来辅助我们实现一个边框样式的动画过渡. 简述 transition 定义和用法 语法 实现 效果 源码 transition 定义和用 ...