我正在尝试一个程序来搜索指定区域内的文本文件中的单词。 考虑一个文本为:
的文本文件dynamic means changing
change good
tickme 1
python is good
its dynamic
and precise
tickme 2
its rapid
its best
well and easy
我有一个包含' dynamic'我需要搜索动态的'在' tickme' s只有。如果动态是与#tick;'然后它必须输出为:
tickme 1
its dynamic
我尝试了编码,只能打印“动态”字样。从整体文件,但我只需要在tickme.Please帮助!
mylist = []
with open('programt.txt','r') as file:
for line in file:
for key in mylist:
if key in line:
print line
请帮助!答案将不胜感激..
答案 0 :(得分:1)
tickme=""
with open('a.txt','r') as file:
for line in file:
if 'tickme' in line:
tickme=line
if line.startswith(' '):
if 'dynamic' in line:
print searchstring
print line
解释:
将tick me值存储在tickme
我们知道在tickme内部的行以一个开头的空格开头,与第一行不同