python统计文档中词频的小程序

python版本2.7

效果如下:

程序如下,测试文件与完整程序在我的github

 #统计空格数与单词数 本函数只返回了空格数 需要的可以自己返回多个值
def count_space(path):
number_counts = 0
space_counts = 0
number_list = [] with open(path, 'r') as f:
for line in f:
line = line.strip()
space_split_list = line.split(' ')
space_counts += len(space_split_list) - 1
for word in space_split_list:
if word.isdigit():
number_list.append(word)
number_counts = len(number_list) return space_counts
#大写转小写 过滤特殊字符等
def count_word(path):
result = {}
with open(path) as fileread:
alltext = fileread.read() alltext = alltext.lower() alltext = re.sub("\"|,|\.", "", alltext) for word in alltext.split():
if word not in result:
result[word] = 0
result[word] += 1 return result def sort_by_count(d): d = collections.OrderedDict(sorted(d.items(), key = lambda t: -t[1]))
return d if __name__ == '__main__':
try:
filename = 'read.txt' dword = count_word(filename)
dword = sort_by_count(dword) countspace = count_space(filename)
print "space_counts", countspace
count_word(filename)
for key,value in dword.items():
print key + ":%d" % value except IOError:
print 'cannot open file %s for read' % filename

python统计文档中词频的更多相关文章

  1. java统计文档中相同字符出现次数(超详细)

    public class test { public static void main(String[] args) throws Exception { InputStream file = new ...

  2. Python帮助文档中Iteration iterator iterable 的理解

    iteration这个单词,是循环,迭代的意思.也就是说,一次又一次地重复做某件事,叫做iteration.所以很多语言里面,循环的循环变量叫i,就是因为这个iteration. iteration指 ...

  3. python读入文档中的一行

    从文件log_fusion中读入数据 方法1 f = open("log_fusion.txt") # 返回一个文件对象 line = f.readline() # 调用文件的 r ...

  4. 利用python处理文档中各字段出现的次数并排序

    import string path = 'waldnn' with open(path,'r') as text: words = [raw_word.strip(string.punctuatio ...

  5. 教你用java统计目录下所有文档的词频

    本文是统计目录下所有文档的词频top10,非单个文档,包含中文和英文. 直接上代码: package com.huawei.wordcount; import java.io.BufferedRead ...

  6. 2018-10-04 [日常]用Python读取word文档中的表格并比较

    最近想对某些word文档(docx)的表格内容作比较, 于是找了一下相关工具. 参考Automate the Boring Stuff with Python中的word部分, 试用了python-d ...

  7. 用python从符合一定格式的txt文档中逐行读取数据并按一定规则写入excel(openpyxl支持Excel 2007 .xlsx格式)

    前几天接到一个任务,从gerrit上通过ssh命令获取一些commit相关的数据到文本文档中,随后将这些数据存入Excel中.数据格式如下图所示 观察上图可知,存在文本文档中的数据符合一定的格式,通过 ...

  8. 使用Python中的HTMLParser、cookielib抓取和解析网页、从HTML文档中提取链接、图像、文本、Cookies(二)(转)

    对搜索引擎.文件索引.文档转换.数据检索.站点备份或迁移等应用程序来说,经常用到对网页(即HTML文件)的解析处理.事实上,通过 Python语言提供的各种模块,我们无需借助Web服务器或者Web浏览 ...

  9. 【python】使用HTMLParser、cookielib抓取和解析网页、从HTML文档中提取链接、图像、文本、Cookies

    一.从HTML文档中提取链接 模块HTMLParser,该模块使我们能够根据HTML文档中的标签来简洁.高效地解析HTML文档. 处理HTML文档的时候,我们常常需要从其中提取出所有的链接.使用HTM ...

随机推荐

  1. JS实现九九乘法表和时间问候语

    编码 小练习,练习使用循环实现一个九九乘法表 第一步,最低要求:在Console中按行输出 n * m = t 然后,尝试在网页中,使用table来实现一个九九乘法表 <!DOCTYPE htm ...

  2. sql:查询创建表的结构

    --显示所有用户表: --1 SELECT SCHEMA_NAME(schema_id) As SchemaName , name As TableName from sys.tables ORDER ...

  3. linux 共享目录

    1. 文件上传遇到多物理机多实例时,怎么处理呢? ftp, 文件同步, 目录共享 这里用目录共享来一把. 2. 操作流程 物理机两台 192.168.1.88  192.168.1.166 2.1 服 ...

  4. Idea15 常用设置(一):JDK、SVN

      1:显示行号  File->Settings->General->Appearance 2: 代码自动补齐即使是小写字母也会弹出代码补齐提示     3:自动编译 设置 4: 设 ...

  5. awk日志分析

    前言 今天我们来讲讲如何用awk进行网站日志分析,得到页面平均耗时排行 文件 [xingxing.dxx@30_28_6_20 ~]$ cat logs /Oct/::: +] GET /pages/ ...

  6. ArcGIS for JavaScript 关于路径开发的一些记录(三)

    最近被一个bug困扰了两天~ 我新发布了一个NAserver(路径分析服务),但是放在之前的代码里面发现不能生成路径.经过我的调试发现并没有代码并没有报错,并且能够返回所生成路径的Graphic la ...

  7. Python爬虫教程-19-数据提取-正则表达式(re)

    本篇主页内容:match的基本使用,search的基本使用,findall,finditer的基本使用,匹配中文,贪婪与非贪婪模式 Python爬虫教程-19-数据提取-正则表达式(re) 正则表达式 ...

  8. BitmapFactory 读取图片方法总结

    ①decodeFile(java.lang.String pathName)     ②decodeResource(android.content.res.Resources res, int id ...

  9. Gitlab命令行简单使用

    使用Gitlab拉取远程文件到本地,然后再创建新分支的流程: git clone url   #将远程分支拉取到本地: git status / git branch #查看git的状态和分支情况,g ...

  10. phantomjs rendering

    http://wwwy3y3.ghost.io/pageres-phantomjs-capture-sreenshot-chinese-fonts-not-render-correctly/ 在使用中 ...