파일의 특정 단어를 집계하고 싶습니다.파이썬에서 특정 단어 하나를 세는 방법?
예를 들어 파일에 '사과'가 몇 번이나 표시됩니까? '사과'와 '단어'를 대체하여
#!/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
,하지만 여전히 내 파일에있는 모든 단어를 계산 : 나는이 시도.
모든 조언을 주시면 감사하겠습니다. : 당신은 단지 하나의 단어의 발생을 걱정하기 때문에
확인을위한
Counter
사전을 사용할 수 있습니다 15083119/python-find-the-a-word-in-a-file), [특정 단어를 파일에 포함] (http://stackoverflow.com/questions/29213458/count-specific- word-in-file-with) – davedwards