【发布时间】:2016-07-15 16:34:27
【问题描述】:
我想统计文件中的特定单词。
例如“apple”在文件中出现了多少次。 我试过这个:
#!/usr/bin/env python
import re
logfile = open("log_file", "r")
wordcount={}
for word in logfile.read().split():
if word not in wordcount:
wordcount[word] = 1
else:
wordcount[word] += 1
for k,v in wordcount.items():
print k, v
通过将“word”替换为“apple”,但它仍会计算我文件中所有可能的单词。
任何建议将不胜感激。 :)
【问题讨论】:
标签: python