我有一个带有一些关键词的文件,用大写字母书写。在大写文本之后,可以有更多文本,但每次都有一个以“Test:”开头的部分。 我应该在“Test:”之后开始这个文本,直到有两个换行符(\ n \ n)。
示例:
ESC
Here can be something.
Test: Here is the definition.
Here can also be some text.
TAB
Here can be something.
Test: Here is the definition.
There can be some more lines.
Here can also be some text.
所以,如果我搜索“TAB”,我想打印出“这是定义。\可以有更多行。”我怎样才能做到这一点?
感谢您的帮助! :)
编辑: 此刻,我搜索等于“TAB”的行。即我有行号,但如何继续?
答案 0 :(得分:1)
导入sys
s="""ESC
Here can be something.
Defn: Here is the definition.
Here can also be some text.
TAB
Here can be something.
Defn: Here is the definition.
There can be some more lines.
Here can also be some text."""
if __name__ == '__main__':
# search = sys.argv[1]
search = 'TAB' #Sample search
index = s.find(search) # Find the index of search term
def_index = s.find('Defn', index) # Find the index of 'Defn'
end_index = s.find('\n\n', def_index) # Find the end of definition
print index , def_index, end_index
print s[def_index: end_index][5:] # 5 is len('Defn:')