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. USACO Section 3.1: Score Inflation

    完全背包问题 /* ID: yingzho1 LANG: C++ TASK: inflate */ #include <iostream> #include <fstream> ...

  2. 超大整数运算算法——为RSA加密算法提供运算工具

    /* program: Large integer operations * Made by:  Daiyyr * date:  2013/07/09 * This software is licen ...

  3. CSS选择符类型

    一.标签选择符:针对某一类标签,可以以标签作为选择符 <style type="text/css"> p{color:#F00; font-size:36px;} &l ...

  4. UVa 10361 Automatic Poetry

    Automatic Poetry Input: standard input Output: standard output Time Limit: 2 seconds Memory Limit: 3 ...

  5. HDU 2144 (最长连续公共子列 + 并查集) Evolution

    我发现我一直理解错题意了,这里的子序列指的是连续子序列,怪不得我写的LCS一直WA 顺便复习一下并查集 //#define LOCAL #include <iostream> #inclu ...

  6. 51nod1354 选数字

    01背包tle. 解题报告(by System Message) 类似于背包的DP,以乘积为状态.先把等选数字里面不是K约数的去掉.然后找出K的约数,进行离散化.然后dp[i][j]表示前i个数字乘积 ...

  7. mysql免安装版安装

    MySQL是一个小巧玲珑但功能强大的数据库,目前十分流行.但是官网给出的安装包有两种格式,一个是msi格式,一个是zip格式的.很多人下了zip格式的解压发现没有setup.exe,面对一堆文件一头雾 ...

  8. python练习程序(批量重命名)

    # -*- coding: cp936 -*- import sys,os,string d=0; path="F://test" srcfile=os.listdir(path) ...

  9. Linux编程(获取系统时间)

    #include <stdio.h> #include <time.h> int main() { time_t now; struct tm *w; time(&no ...

  10. windows下用mingw编译linux项目

    1.下载安装mingw32 2.将mingw下bin和msys\1.0下bin设置为系统path 3.启动msys.bat 4.cd到项目目录 5.输入./configure 6.输入make 7.输 ...