我有一個問題,我似乎無法解決;如果這是重複的道歉,但永遠不會有真正的答案。我從配置文件中提取特定的信息,以文本塊的形式顯示信息,我只需要打印特定的塊,而不需要標題。因此,例如,(與下面的文本格式),我只會想捕捉低於頭2的信息,但沒有什麼過去頭3:Python 3.x打印特定標題後的行數
# 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()
這是極好的感謝,如果我也想打印在該行的對象,我會怎樣去做。我嘗試分割它並打印行[0]以獲得'3'。 line sample =「3 man enable none」,但沒有運氣不斷返回一個沒有對象,也許我不理解的東西。 – onxx