答案:关于“为什么输出错误”的问题 - 您需要遍历行中的每个单词。
建议:
当您搜索多个单词时,您可以将它们放在一个字典中并将计数存储为相应字典键的值。
文件内容:
Hi this is hello
Hello is my name
然后
text_file.read()
会给,
['Hi this is hello\n', 'Hello is my name\n']
text_file.read().splitlines()
['Hi this is hello', 'Hello is my name']
然后拆分行中的每一行,
lines = map(str.split,text_file.read().splitlines())
[['Hi', 'this', 'is', 'hello'], ['Hello', 'is', 'my', 'name']]
在链接可迭代对象时,
it.chain.from_iterable(map(str.split,text_file.read().splitlines()))
['Hi', 'this', 'is', 'hello', 'Hello', 'is', 'my', 'name']
还有,
search=['dog','cat'] # the words that you need count
search = dict.fromkeys(search,0) # will give a dict as {'dog':0,'cat':0}
因此对于您的问题,
def main():
text_file = open("textfile.txt", "r")
search=['cat','dog']
search = dict.fromkeys(search,0)
import itertools as it
res=dict()
for word in it.chain.from_iterable(map(str.split,text_file.read().splitlines())):
if word.lower() in search:
search[word.lower()]=search[word.lower()]+1
for word,count in search.iteritems():
print('the word %s occurs %d times'%(word,count))
这也可以计算区分大小写的单词!
希望对你有帮助!