自己初步想到这种程度,如果某个大牛看到,给我优化下程序,最后各种情况都能考虑到的,包括含有标点符号,特殊字符,异常处理等等。谢谢啦.....
def main():
word = raw_input('Input your world: ').strip()count = 0
with open('123.txt') as f:
for line in f:
count += line.split().count(word)
print count
def getNum(file, word):
word = word.strip()
count = 0
with open(file)as f:
for line in f:
count += line.split().count(word)
return count
if __name__ == "__main__":
print getNum('123.txt', 'hello')