python 2.7 - Count how many times a word appears in a text file -
def paraula(file,wordtofind): f = open(file,"r") text = f.read() f.close() count = 0 in text: s = i.index(wordtofind) count = count + s return count paraula (file,wordtofind)
def paraula(file,wordtofind): f = open(file,"r") text = f.read() f.close() count = 0 index = text.find(wordtofind) # returns index of first instance of word while index != -1: count += 1 text = text[index+len(wordtofind):] # cut text starting after word index = text.find(wordtofind) # search again return count paraula (file,wordtofind)
Comments
Post a Comment