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)
前一篇:Python文件读写操作
后一篇:Python文档操作

加载中…