我想计算文件中的特定单词。如何计算Python中的一个特定单词?
例如'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
与“苹果”取代“字”,但也应算做我的文件中的所有可能的单词。
任何意见将不胜感激。 :)
这些退房查看:蟒蛇 - 找到这个词的一个文件中出现(http://stackoverflow.com/问题/ 15083119/python-find-the-the-word-in-a-file),[用文件计算特定单词](http://stackoverflow.com/questions/29213458/count-specific- word-in-file-with) – davedwards