codehacker
codehacker

Reputation: 320

for loop Execution

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

Upvotes: 1

Views: 88

Answers (1)

ewwink
ewwink

Reputation: 19184

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 

Upvotes: 1

Related Questions