1

I have two for loop. The outer loop reads from a text file and enters another for loop, which reads from a different text file when there is an exception. The inner loop should exit the loop and then iterate to the next element in the outer loop but again, once iterated it should continue from where it stopped in the inner loop.

Any idea as to how to do it in python?

following is the code:

with open('E:\marr.txt') as f:
    content = f.readlines()
content = [x.strip() for x in content]
with open('E:\prlist.txt') as f:
    content1 = f.readlines()
content1 = [x.strip() for x in content1]
with open('E:\Master\master1.csv', 'a') as f:
    headers = ("Source Link,Company Link,company name")
    f.write(headers)
    f.write("\n")
    for ip in content1:
        chrome_options.add_argument('--proxy-server=%s' % ip)
        try:
            for link in content:

                    try:
                        browser.get(link)
                        browser.implicitly_wait(4)
                        abba = browser.find_element_by_css_selector('.WebToolsetToolWebPart_Cntnr.WebToolsetToolWebPart_Aligned_LEFT.WebToolsetToolWebPart_TxtTool_Cntnr')
                        aas = abba.text
                        aa = aas.replace(",","")
                        print(ip + "," + link + "," + aa)
                        f.write(link + "," +aa+"\n")

                    except NoSuchElementException:
                        aa = "no count available"
                        print(ip + ","+link + "," + aa)
                        f.write(link + "," + aa + "\n")
        except StaleElementReferenceException:
            pass
4
  • Add your code here. It'll be easy to understand Commented Dec 27, 2018 at 5:07
  • @NikithaNadig pfb the code Commented Dec 27, 2018 at 5:09
  • I am unsure I understand your intention correctly: but if you want to continue the inner loop from its current progress, you should not use a for loop. Just iterate manually, initialising c=iter(content) outside the loops and calling link=next(c) inside, i.e. driving the iteration protocol directly Commented Dec 27, 2018 at 5:36
  • so how do we execute it@Pynchia Commented Dec 27, 2018 at 5:42

1 Answer 1

1

save inner loop index, if error continue from this, see currentIndex for how it work.

with open('E:\Master\master1.csv', 'a') as f:
    headers = ("Source Link,Company Link,company name")
    f.write(headers)
    f.write("\n")

    currentIndex = 0
    for ip in content1:
        chrome_options.add_argument('--proxy-server=%s' % ip)
        try:
            for link in content[currentIndex:]: # start from 0 or continue from last index error
                try:
                    browser.get(link)
                    browser.implicitly_wait(4)
                    abba = browser.find_element_by_css_selector('.WebToolsetToolWebPart_Cntnr.WebToolsetToolWebPart_Aligned_LEFT.WebToolsetToolWebPart_TxtTool_Cntnr')
                    aas = abba.text
                    aa = aas.replace(",","")
                    print(ip + "," + link + "," + aa)
                    f.write(link + "," +aa+"\n")

                except NoSuchElementException:
                    aa = "no count available"
                    print(ip + ","+link + "," + aa)
                    f.write(link + "," + aa + "\n")
                    break # stop this inner loop and continue outer loop

                # current loop is good save the next index
                currentIndex += 1

            # if it last index of "content", reset the index <- minor fix
            if currentIndex == len(content) - 1:
                currentIndex = 0

        except StaleElementReferenceException:
            pass 
Sign up to request clarification or add additional context in comments.

1 Comment

you're welcome, I edited the answer, fix reset index.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.