下面是一个简单的搜索。它可以工作,除了iterator
跳过文件的第一行。内部iterator
第一个print
语句有正确的单词,但第二个print
语句(for
循环之后)有第二行文本,而不是第一行。
for
我错过了这个循环行为怎么办?
"""Searches for the query inside a file
"""
def lines(the_file, query):
lines = open(the_file)
line(lines, query)
def line(lines, query):
line = lines.readline()
iterator(line, lines, word, query)
def word(line, query):
word = line.strip()
conditional(query, word)
def iterator(this, that, function, query):
print this
for this in that:
print this
function(this, query)
def conditional(this, that):
if this in that:
output(that, True)
else:
None
def output(query, result):
print query
def search(the_file, query):
lines(the_file, query)
search('c:/py/myfile.txt', 'a')