Pre-defining an action to take when an expected error occurs

Tempo bradfordh at gmail.com
Thu Sep 14 22:40:35 EDT 2006


Hello. I am getting the error that is displayed below, and I know
exactly why it occurs. I posted some of my program's code below, and if
you look at it you will see that the error terminates the program
pre-maturely. Becasue of this pre-mature termination, the program is
not able to execute it's final line of code, which is a very important
line. The last line saves the Excel spreadsheet. So is there a way to
make sure the last line executes? Thanks in advanced for all of the
help. Thank you.


Error
####

IndexError: list index out of range


Code Sample
###########

for rx in range(sh.nrows):
    rx = rx +1
    u = sh.cell_value(rx, 0)
    u = str(u)
    if u != end:
        page = urllib2.urlopen(u)
        soup = BeautifulSoup(page)
        p = soup.findAll('span', "sale")
        p = str(p)
        p2 = re.findall('\$\d+\.\d\d', p)
        for row in p2:
            ws.write(r,0,row)
                    
w.save('price_list.xls')




More information about the Python-list mailing list