Reputation: 420
read_file = open ('C:\Users\Mahya\Desktop\\automate\Autosupports\\at1.txt','r')
content = read_file.readlines()
for line in content:
if line.contains('===== BOOT TIME STATS ====='):
print found
I want to read '===== BOOT TIME STATS ====='
this line and print the lines that are below till next line
please help
Upvotes: 0
Views: 117
Reputation: 56694
def lineRange(lines, start, end):
"""Return all lines from the first line containing start
up to (but not including) the first ensuing line containing end
"""
lines = iter(lines)
# find first occurrence of start
for line in lines:
if start in line:
yield line
break
# continue until first occurrence of end
for line in lines:
if end in line:
break
else:
yield line
def main():
fname = 'C:/Users/Mahya/Desktop/automate/Autosupports/at1.txt'
start = '===== BOOT TIME STATS ====='
end = start # next section header?
with open(fname) as inf:
lr = lineRange(inf, start, end)
try:
lr.next() # skip header
print ''.join(lr)
except StopIteration:
print 'Start string not found'
if __name__=="__main__":
main()
Upvotes: 0
Reputation: 6918
file = open ('C:\Users\Mahya\Desktop\\automate\Autosupports\\at1.txt','r')
for line in file:
if '===== BOOT TIME STATS =====' in line: break
for line in file:
if 'i wanna stop here' in line: break
print line
Upvotes: 0
Reputation: 58958
I'm guessing you want to print the lines between the first and second occurrences of the given string:
read_file = open ('C:\Users\Mahya\Desktop\\automate\Autosupports\\at1.txt','r')
content = read_file.readlines()
found = False
for line in content:
if line.contains('===== BOOT TIME STATS ====='):
if found:
break # Exit the for loop when finding the string for the second time
found = True
if found:
print line
Upvotes: 1
Reputation: 36071
Without testing:
read_file = open ('C:\Users\Mahya\Desktop\\automate\Autosupports\\at1.txt','r')
content = read_file.readlines()
i_found_the_block = False
for line in content:
if "===== BOOT TIME STATS =====" in line:
print ("found")
i_found_the_block = True
if i_found_the_block:
print line
Upvotes: 1