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. HTML语言中img标签的alt属性和title属性的作用与区别

    alt属性是在你的图片因为某种原因不能加载时在页面显示的提示信息,它会直接输出在原本加载图片的地方,而title属性是在你鼠标悬停在该图片上时显示一个小提示,鼠标离开就没有了,有点类似jQuery的h ...

  2. VUE的两种跳转push和replace对比区别

    router.push(location) 在vue.js中想要跳转到不同的 URL,需要使用 router.push 方法. 这个方法会向 history 栈添加一个新的记录,当用户点击浏览器后退按 ...

  3. sql server or Oracle: table MS_Description

    --SQL Server表描述 及 字段描述的增.删.改.查询 --sql server 2000系统表sysproperties在SQL 2008中无效的问题 今天无意中在网上发现Sqlserver ...

  4. vue-router 的使用

    vue-router 是 vue  的  一个特色. 下面介绍vue-router 的使用: 一.先将vue-router作为vue 的一个插件使用 import Vue from 'vue' imp ...

  5. 使用原生的javascript来实现轮播图

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. angular attrs.$observe和$scope.$watch的区别

    http://stackoverflow.com/questions/14876112/difference-between-the-observe-and-watch-methods https:/ ...

  7. Apache2.4和IIS7整合共享80端口测试

    言我再重新排版一下 在C:\Windows\System32\drivers\etc\hosts文件中配置2个测试域名用于整合测试 127.0.0.1 www.aaa.com // apache项目 ...

  8. 关于Excel文件导入到Sqlserver2008中出现截断错误的解决办法

    出现错误的可能原因: 1.数据库字段Varchar长度不够: 2.不能用Text类型: 3.数据中可能存在换行符: 4.数据项文本过长,超过4000: 5.前8行的最大长度不够大,后面有超过的. 解决 ...

  9. iOS文件处理类

    iOS文件处理类 这是一个用来简化iOS中关于文件操作的一个类,所有方法都为类方法. Source File.h // // File.h // FileManager // // http://ho ...

  10. CPP11实践

    - 001 以lambda作为返回值该如何声明? 标准库提供了function模板可以直接声明,如std::function<void (int, int)>.如下函数foo返回了一个函数 ...