0
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

1
  • 1
    What do you mean by print the lines that are below till next line? Commented Mar 7, 2011 at 12:19

4 Answers 4

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

If you opened the file in a with-statement, and iterate over the file directly instead of using readlines(), this would be fine.
0
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

Comments

0
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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.