加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Python统计一个文档中每个单词的出现次数

(2018-04-30 15:34:23)
标签:

统计文档单词出现次数

python

分类: Python

import string
#统计一个文档中每个单词的出现次数
def process_file(filename):
h=dict()
fp=open(filename)
for line in fp:
process_line(line,h)
return h

def process_line(line,h):
line=line.replace('-',' ')

for word in line.split():
word=word.strip(string.punctuation+string.whitespace)
word=word.lower()
h[word]=h.get(word,0)+1

hist=process_file(r'c:\work\text.txt')
print(hist)

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有