自己初步想到这种程度,如果某个大牛看到,给我优化下程序,最后各种情况都能考虑到的,包括含有标点符号,特殊字符,异常处理等等。谢谢啦.....
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')
本文介绍了一个简单的Python程序,用于从指定文件中搜索特定单词出现的次数。该程序考虑了基本的输入处理,但仍有改进空间,例如处理标点符号和其他特殊字符。
1661

被折叠的 条评论
为什么被折叠?



