import re txt = open("123.txt", "r").read() print len(re.findall("hello", txt))
第3个回答 2017-06-13
content = {} wth open("文件") as fr: for line in fr: lines = line.strip().split(" ") #假设单词与单词之间,空格做为分隔符 for word in lines: if word not in content: content[word] = 0 content[word] += 1 for word,val in content.items(): print '%s:%d\n"%(word,val)