如何用Python实现任一个英文的纯文本文件,统计其中的单词出现的个数?

来源:互联网 发布:手机健康体检软件 编辑:程序博客网 时间:2024/05/23 10:40
import refile_name = 'test.txt'lines_count = 0words_count = 0chars_count = 0words_dict  = {}lines_list   = []with open(file_name, 'r') as f:    for line in f:        lines_count = lines_count + 1        chars_count  = chars_count + len(line)        match = re.findall(r'[^a-zA-Z0-9]+', line)        for i in match:            # 只要英文单词,删掉其他字符            line = line.replace(i, ' ')        lines_list = line.split()        for i in lines_list:            if i not in words_dict:                words_dict[i] = 1            else:                words_dict[i] = words_dict[i] + 1print 'words_count is', len(words_dict)print 'lines_count is', lines_countprint 'chars_count is', chars_countfor k,v in words_dict.items():    print k,v

0 0
原创粉丝点击