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. react相关知识点链接整理

    1.React组件之间的通信 2.中间件做代理解决跨域问题 3.不要再问我跨域的问题了 4.React 组件数据流 && 组件间沟通 5.如何理解虚拟DOM 6.react性能调谐与d ...

  2. HDU4336:Card Collector(min-max容斥)

    题面 传送门 Sol 方法一 直接状压就好了 # include <bits/stdc++.h> # define RG register # define IL inline # def ...

  3. html+css模仿的锤子官方首页

    <div id="wrapper"> <header> <div class="container"> <h1> ...

  4. SSIS 使用OLEDB/ADO NET Source 数据流source控件 连接Oracle失败

    在做数据提取的时候发现一个非常奇怪的问题. Oracle客户端是安装正确并且Toad可以正常运行的,但是在新建OLEDB/ADO NET Source 数据流source控件连接Oracle的时候一直 ...

  5. MySQL数据库(1)----入门级操作

    1.在服务器主机上以 root 用户登陆,创建位于其他客户端的新用户: mysql> CREATE USER 'newuser'@'192.168.1.109' IDENTIFIED BY 'p ...

  6. opencv 从摄像头中读取视频并保存(c++版)

    原文:http://blog.csdn.net/zhongshijunacm/article/details/68947890 OpenCV中的视频操作函数如下表所列: VideoCapture Vi ...

  7. linux 系统开机自启执行 操作的配置

    1 linux 服务注册 service文件 在service文件中设置变量和环境变量 [Unit] Description= #服务描述 After=syslog.target #服务启动依赖 [S ...

  8. python wsgi PEP333 中文翻译

    PEP 333 中文翻译 首先说明一下,本人不是专门翻译的,英文水平也不敢拿来献丑.只是这是两年前用python的时候为了自己学习方便而翻译的,记录着笔记自己看看而已.最近翻出来看看觉得还是放出来吧. ...

  9. mac 修改MAC代码

    1.生成一个mac地址: openssl rand -hex | sed 's/\(..\)/\1:/g; s/.$//' 2.关闭无线 sudo /System/Library/PrivateFra ...

  10. Log4Net使用教程

    简介 为方便跟踪程序运行情况,我们可以记录系统运行异常日志,winform和web都可以通过继承异常或者try来实现. 官方网站:http://logging.apache.org/log4net/ ...