我不知道为什么要递归调用该函数。
如果我答对了你的问题,你想存储你得到的行,直到你找到某个关键字。一旦你点击了关键字,你就想中断,你需要一个变量来记录你读到的行。
可以使用以下代码执行此操作:
with open("file.txt", "r") as f:
extracted_line = [] # create an empty list to store the extracted lines
for line in f:
if 'REQUIREMENT TYPE' in line: # if keyword is present in the current line, break
break
else:
extracted_line.append(line) # else, append the line to store them later
stored_lines = ''.join(extracted_line) # variable which stores the lines till keyword
print stored_lines
f.close()
我在代码旁边加了注释。希望这能回答你的问题。如果您需要任何澄清,请告诉我。