Python 统计文本中单词的个数
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 统计文本中单词的个数的更多相关文章
- python统计文本中每个单词出现的次数
.python统计文本中每个单词出现的次数: #coding=utf-8 __author__ = 'zcg' import collections import os with open('abc. ...
- java统计文本中单词出现的个数
package com.java_Test; import java.io.File; import java.util.HashMap; import java.util.Iterator; imp ...
- 统计文件中单词的个数---Shell及python版
最近在看shell中有个题目为统计单词的个数,使用了awk功能,代码如下 #!/bin/bash ];then echo "Usage:basename $0 filename" ...
- shell统计文本中单词的出现次数
Ubuntu14.04 给定一个文本,统计其中单词出现的次数 方法1 # solution 1 grep与awk配合使用,写成一个sh脚本 fre.sh sh fre.sh wordfretest.t ...
- HDU_2030——统计文本中汉字的个数
Problem Description 统计给定文本文件中汉字的个数. Input 输入文件首先包含一个整数n,表示测试实例的个数,然后是n段文本. Output 对于每一段文本,输出其中的汉 ...
- JAVA实验--统计文章中单词的个数并排序
分析: 1)要统计单词的个数,就自己的对文章中单词出现的判断的理解来说是:当出现一个非字母的字符的时候,对前面的一部分字符串归结为单词 2)对于最后要判断字母出现的个数这个问题,我认为应该是要用到ma ...
- 使用xargs同步文本中单词出现个数
#!/bin/bash # 分析一个文本文件中单词出现的频率. # 使用 'xargs' 将文本行分解为单词. # 检查命令行上输入的文件. ARGS= E_BADARGS= E_NOFILE= if ...
- C语言算法--统计字符串中单词的个数
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(void) { int le ...
- Python 基础 - 统计文本里单词的个数以及出现的次数
# -*- coding:utf-8 -*- #author:V def tol (file1,gui): #写一个方法,定义文件,or 匹配规则 import re patt = re.compil ...
随机推荐
- 【linux】/dev/null与/dev/zero详解【转】
转自:http://www.cnblogs.com/xianghang123/archive/2012/03/23/2413381.html 使用/dev/null 把/dev/null 看作&quo ...
- android 更改avd路径
第一种方法,适合还没有建立 AVD 的情况 即:在计算机右击的属性 选择环境变量,然后添加一个用户的环境变量,名字为 "ANDROID_SDK_HOME”,然后把变量值改为你想将" ...
- 用xshell操作linux系统的常用命令
(1)命令ls——列出文件 ls -la 给出当前目录下所有文件的一个长列表,包括以句点开头的“隐藏”文件 ls a* 列出当前目录下以字母a开头的所有文件 ls -l *.doc 给出当前目录下以. ...
- 选择——ERP信息系统选型
做一次选择并不难,难的是做一次坚定而正确的选择.TCL电脑公司的ERP软件选型就是一次正确而艰难的选择过程.让我们从头说起吧! 业界都知道TCL电脑是IT行业的新入行者,更知道TCL的另一个诠释:& ...
- Mysql中的Prepared Statement与Stored Precedure学习
可以参考: http://stackoverflow.com/questions/196652/prepared-statement-vs-stored-procedure They are not ...
- [ionic开源项目教程] - 第14讲 ionic解决跨域问题
[ionic开源项目教程] 第14讲 使用jsonp解决跨域问题 相信很多朋友在开发中都会遇到以下这个问题. No 'Access-Control-Allow-Origin' header is pr ...
- 解同余式ax ≡ c(mod m)
将式子变形为 ax-c=my 可以看出原式有解当且仅当线性方程ax-my=c有解 设g = gcd(a, m) 则所有形如ax-my的数都是g的倍数 因此如果g不整除c则原方程无解. 下面假设g整除c ...
- 事件类型: 错误 事件来源: Service Control Manager 事件种类: 无 事件 ID: 7000
在控制面板\管理工具\服务里找dns Client 服务,把他启动了
- 20160125.CCPP详解体系(0004天)
程序片段(01):宽字符.c 内容概要:宽字符 #include <stdio.h> #include <stdlib.h> #include <Windows.h> ...
- scala学习笔记(7):函数(1)
函数是Scala的第一公民! 1 基本定义 scala> def max(x: Int, y: Int): Int = { if (x > y) x else y } 跟着是括号里带有冒 ...