Python Forum
Skipping line in text without Restarting Loop - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Skipping line in text without Restarting Loop (/thread-36838.html)



Skipping line in text without Restarting Loop - IdMineThat - Apr-05-2022

I'm breaking apart a text string line by line with a for loop. Is it possible to get to the next line, without starting at the beginning of the loop?


import requests

webpage=((requests.get('http://SomeRandomWebPage.com')).text)

Counter = 0
for line in webpage.split('\n'):
    Counter = Counter + 1
    print("Doing Stuff Here")
    if Counter == 8:
        ##I want to read the next line from webpage here, without starting at the beginning of the for loop
        print("Do Different Stuff Here")
        continue



RE: Skipping line in text without Restarting Loop - deanhystad - Apr-05-2022

You can use next() to get the next item from an iterator. Here I use next() to skip odd values.
x = iter(range(10))
while True:
    if (num := next(x, None)) is None:
        break
    print(num, end=" ")
    if num % 2 == 0:
        next(x)
Output:
0 2 4 6 8
I don't know that I've ever seen next() using the same iterator as a for loop, but it doesn't appear to do any harm. Here next() gets the last value from x, but the for loop catches the StopIteration exception like normal.
x = iter(range(10))
for num in x:
    print(num, end=" ")
    if num == 8:
        next(x)
Output:
0 1 2 3 4 5 6 7 8



RE: Skipping line in text without Restarting Loop - bowlofred - Apr-05-2022

You could make an explicit iterator from the list returned from split(), then you can loop over the iterator, but also pull from it inside the loop.

Here's a loop that prints every word in a list of words, but if the word starts with "t", then it also tacks on the next word in the list.
Note that if the last word in the list started with "t", the next() would generate an error.

mytext = "This is a list of words that can be iterated."

words = iter(mytext.split())  # words holds an iterator of the list
for word in words:  # For loop will read most of the iterator
    if word.lower().startswith("t"):
        next_word = next(words)  # next() can also consume from the iterator
        print(f" {word} / {next_word}")
    else:
        # If the word doesn't start with "t", print it alone
        print(word)
Output:
This / is a list of words that / can be iterated.



RE: Skipping line in text without Restarting Loop - IdMineThat - Apr-05-2022

(Apr-05-2022, 03:24 AM)bowlofred Wrote: You could make an explicit iterator from the list returned from split(), then you can loop over the iterator, but also pull from it inside the loop.

Here's a loop that prints every word in a list of words, but if the word starts with "t", then it also tacks on the next word in the list.
Note that if the last word in the list started with "t", the next() would generate an error.

mytext = "This is a list of words that can be iterated."

words = iter(mytext.split())  # words holds an iterator of the list
for word in words:  # For loop will read most of the iterator
    if word.lower().startswith("t"):
        next_word = next(words)  # next() can also consume from the iterator
        print(f" {word} / {next_word}")
    else:
        # If the word doesn't start with "t", print it alone
        print(word)
Output:
This / is a list of words that / can be iterated.

You are a god. I got it working AND learned something.

Thanks to both of you.


RE: Skipping line in text without Restarting Loop - deanhystad - Apr-05-2022

You can provide a default value for next() to return when the a StopIteration exception occurs. For bowlofred's example you would use: next_word = next(words, "")