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 ...
随机推荐
- Eclipse无法通过FileExplore打开真机data目录
ok ——> Eclipse无法通过FileExplore打开真机data目录 ref:http://blog.csdn.net/koyoter/article/details/7657440 ...
- Solr的一些查询参数
fl: 是逗号分隔的列表,用来指定文档结果中应返回的 Field 集.默认为 “*”,指所有的字段. defType: 指定query parser,常用defType=lucene, defType ...
- 38-语言入门-38-Coin Test
题目地址: http://acm.nyist.net/JudgeOnline/problem.php?pid=204 描述As is known to all,if you throw a co ...
- linux 防火墙打开端口/屏蔽IP等
开启/禁用 sudo ufw allow|deny [service] 打开或关闭某个端口,例如: sudo ufw allow smtp 允许所有的外部IP访问本机的25/tcp (smtp)端口 ...
- PopupWindwo和AlertDialog的区别
AlertDialog 是非阻塞式对话框:AlertDialog弹出时,后台还可以做事情:而PopupWindow是阻塞式对话框:PopupWindow弹出时, 程序会等 待,在PopupWind ...
- SVN 显示灰色减号代表什么意思
灰色减号(ignored):意思就是忽略的意思,不对其进行版本控制,忽略对其进行的任何操作
- 【分享】哪个OS X版本支持哪个Xcode的版本?
在安装Xcode时,会碰到跟OS X操作系统匹配的问题,对照下下面几个表,以免给自己带来编译不过或者奇怪的错误等问题 以下列表来自网络: Xcode 1.0 - Xcode 2.x (before i ...
- UVa 10815 Andy's First Dictionary
感觉这道题要比之前几个字符串处理的题目难度要大了一些. 题目大意:给若干行字符串,提取出所有单词并去掉重复的,最后按字典顺序输出. 对于输入大致有两种思路,一种是逐个读入字符,遇到字母的话就放到wor ...
- ecshop首页调用评论及图片
1.在library文件夹中建立一个名为index_comment.lbi文件 2.输入以下代码 <meta http-equiv="Content-Type" conten ...
- HDU 产生冠军 2094
解题思路:这题重在分析,可能你知道的越多,这题想得越多,什么并查集,什么有向图等. 事实是,我们会发现,只要找到一个,并且仅有一个的入度为0的点,说明可以找出 冠军.若入度为0的点一个都没有,说明 ...