文件统计
来源:互联网 发布:通达信指标公式源码 编辑:程序博客网 时间:2024/04/30 16:46
统计文件的字数,行数,高频词汇等
'''Created on 2014/09/02@author: wangz'''keep = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' ','-',"'"]def normalize(s): '''Convert s to normalized string.''' result = '' for c in s.lower(): if c in keep: result += c return resultdef normalize2(s): '''Convert s to normalized string.''' return ''.join(c for c in s.lower() if c in keep)def make_freq_dict(s): '''Returns a dictionary whose keys are the words of s, and whose values are the counts of those words. ''' s = normalize(s) words = s.split() d={} for w in words: if w in d: d[w] += 1 else: d[w] = 1 return ddef print_file_stats(fname): '''Print statistics for the given file.''' s = open(fname,'r').read() num_chars = len(s) num_lines = s.count('\n') d = make_freq_dict(s) num_words = sum(d[w] for w in d) lst = [(d[w],w) for w in d] lst.sort() lst.reverse() print("The file '%s' has: "%fname) print(" %s characters"%num_chars) print(" %s lines"%num_lines) print(" %s words"%num_words) print("\nThe top 10 most frequent word are:") i = 1 for count,word in lst[:10]: print('%2s. %4s %s'%(i,count,word)) i += 1 inputfile = raw_input('input a file:')print_file_stats(inputfile)
0 0
- 文件统计
- 文件统计
- 统计文件
- Linux统计单个文件统计
- 流写文件统计
- 文件个数、文件夹统计
- 文件个数、文件夹统计
- 多文件统计字频
- Linux统计文件行数
- Linux统计文件行数
- Linux统计文件行数
- VBA 统计文件个数
- 递归统计文件数
- 统计电脑所有文件
- 统计文件行数
- 文件内容统计
- Linux统计文件行数
- linux统计文件个数
- python
- 第八周项目1-求简单分段函数的值
- 【Android】高低API版本兼容之@TargetApi与@SuppressLint("NewApi")
- [安卓]Android 的 LogCat 的使用
- nsdata与byte转换的问题
- 文件统计
- 第八周项目1—求简单分段函数的值
- iOS 6的Rotation
- ISO8583接口的详细资料
- 第8周项目1-求简单分段函数的值
- median
- 黑马程序员——IO流(2)
- 第八周项目四—个人所得税计算器
- 常规数据类型检测方法