6

I've got a small script that is extracting some text from a .html file.

f = open(local_file,"r")
for line in f:
    searchphrase = '<span class="position'
    if searchphrase in line:
        print("found it\n")

That works fine for me(error handling will be imported later), my problem is that the text I want to extract follows 2 lines after the searchphrase. How can I move 2 lines down in the .html file ?

2 Answers 2

13

You can advance f (which is an iterable) by two lines by calling next() on it twice:

with open(local_file,"r") as f
    for line in f:
        searchphrase = '<span class="position'
        if searchphrase in line:
            print("found it\n")
            next(f) # skip 1 line
            return next(f)  # and return the line after that.

However, if you are trying to parse HTML, consider using a HTML parser instead. Use BeautifulSoup, for example.

Sign up to request clarification or add additional context in comments.

Comments

0

This works nice for me:

f = open(local_file,"r")
found = -1
for line in f:
    if found == 2:
        print("Line: "+line);
        break
    elif found > 0:
        found += 1
    else:
        searchphrase = '<span class="position'
        if searchphrase in line:
            print("found it")
            found = 1

The input file was:

bla
<span class="position">Hello</span>
blub
that's it
whatever

And the output of the program:

found it
Line: that's it

Instead of calling break you may also reset found to -1 to search for more occurences of the pattern...

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.