【发布时间】:2017-10-15 22:43:08
【问题描述】:
我有一个似乎无法解决的问题;抱歉,如果这是重复的,但从未遇到过真正的答案。我正在从配置文件中提取特定信息,该文件以文本块的形式显示信息,我只需要打印特定的块,而不需要标题。因此,例如(使用下面的文本格式)我只想捕获 Header2 下面的信息,而不是标题 3 之后的任何信息:
# output could containmultiple headers, and lines, or no lines per header this is an example of what could be present but it is not absolute.
header1
-------
line1
line2
line3 # can be muiplies availables or known
header2
-------
line1
line2
line3 # can be muiplies availables or known
header3
-------
header4
-------
line1
line2
line3 # can be multiple linnes or none not known
这是我开始使用的代码,但卡在第二个循环布尔值或逻辑上,仅打印该标题块的行:
Raw_file = "scrap.txt"
scrape = open(Raw_file,"r")
for fooline in scrape:
if "Header" in fooline:
#print(fooline) # prints all lines
#print lines under header 2 and stop before header 3
scrape.close()
【问题讨论】:
标签: python python-3.x text printing