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. 文章投稿 latex 生成 pdf的字体Embeded问题解决(转自兵马俑BBS)

    此法可以把所有字体转为Embedded,先生成*.ps文件,下载ghostscrip8.51和GSView4.7 安装,用gsview4.7转换*.ps->*.pdf,在gsview中File- ...

  2. USACO Section 2.3: Zero Sum

    这题我做得比较麻烦,网上有个比较简单的程序. /* ID: yingzho1 LANG: C++ TASK: zerosum */ #include <iostream> #include ...

  3. flex 4 Filters

    <s:RectangularDropShadow id="dropShadow" blurX="10" blurY="10" alph ...

  4. POJ3415 Common Substrings

    后缀数组 求长度不小于k的公共子串的个数 代码: #include <stdio.h> #include <string.h> ; int len, len1; int wa[ ...

  5. 【转载】Javascript原型继承-学习笔记

    阮一峰这篇文章写的很好 http://www.ruanyifeng.com/blog/2011/06/designing_ideas_of_inheritance_mechanism_in_javas ...

  6. HDU 1527 (Wythoff 博弈) 取石子游戏

    对于Wythoff博弈中的两个数列,An和Bn有这样的关系: An + n = Bn, An = floor(φ * n) 所以我们可以根据a b的差值来计算一个新的a出来看看这两个值是否相等. 想等 ...

  7. UVa 537 Artificial Intelligence?

    题目大意:输入一个字符串,根据物理公式P=U*I,已知其中两个量,求第三个量,结果保留两位小数.   Artificial Intelligence?  Physics teachers in hig ...

  8. Linux磁盘占用100%解决方法

    /opt分区被web日志堆满了,导致一些服务无法正常运行,于是rm -fr掉这些日志(近11GB),但是服务仍没有恢复正常,用df -hT看,该分区占用还是100%: [root@anjing opt ...

  9. springmvc+hibernate入门-揭开神秘的面纱

            Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块.使用 Spring 可插入的 MVC 架构,可以选择是使用内置的 Spring Web 框架还是 Struts 这 ...

  10. 原创:无错版!让DEDE只生成一个RSS文件,不分栏目

    DEDE为每一个栏目都独立创建一个rss文件, 如果用户要整站订阅相当不方便.  所以需要修改让dede只生成一个rss. 网上大部分帖子要么是抄, 要么是有问题少了步骤. 今天特意整理下. 分享.. ...