Python 在某些事件发生后查找文本
我有一个文件,里面有一些大写的关键字。在大写文本之后,可以有更多的文本,但每次都有以“Test:”开头的部分。 我应该在“Test:”之后开始此文本,直到出现两个换行符(\n\n) 例如:Python 在某些事件发生后查找文本,python,Python,我有一个文件,里面有一些大写的关键字。在大写文本之后,可以有更多的文本,但每次都有以“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 b
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”,我想打印出“这是定义。\n可能还有更多行。”我该怎么做
谢谢你的帮助!:)
编辑:
现在,我搜索等于“TAB”的行。也就是说,我有行号,但如何继续 导入系统
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:')
[链接]向我们展示您迄今为止所做的尝试?请仔细阅读。