简体   繁体   中英

Python - IndexError: list index out of range - For loop / While loop

I have this string:

string="R$ 35.0123. "

I want to clean that, taking that final dot and that space in the end out. Making it look like this:

string:"R$ 35.0123"

I'm trying to use this:

while string[-1].isdigit()==False:
      string=string[:-1]

However I'm getting this error:

IndexError: list index out of range

The weird thing is that I'm running this inside a for loop and if the looped list has only one item, it works fine. If the list has two items, this problem happens.

Any ideas? Merry xmas for you all.

FULL CODE BELOW (the string is the variable "valor")

if CNPJ in page:
    CNPJloc=[]
    for i in (re.finditer(CNPJ,page)):
        CNPJloc.append(i.start())

    for i in CNPJloc:
        CNPJposition=i
        beg_string=["aviao"]
        end_string="sicon"
        for i in beg_string:
            if i in lower_page[CNPJposition-200:]:
                beg_string=i                
                extrato_de_contrato=page[lower_page.rfind(beg_string,0,CNPJposition):lower_page.find(end_string,CNPJposition)]
                lower_extrato=extrato_de_contrato.lower()
                def valor():
                    valor=["valor do contrato:","valor:"]
                    for i in valor:
                        if i in lower_extrato:
                            valor=extrato_de_contrato[lower_extrato.rfind(i)+len(i):lower_extrato.find("fonte",lower_extrato.rfind(i))]
                        while valor[-1].isdigit()==False:
                            valor=valor[:-1]
                    print("Valor Total: ", valor)
                    return valor
                valor()

Python has functionality built in to do this with rstrip

string.rstrip('. ')

Output

'R$ 35.0123'

Sounds like you just need to remove the last dot and everything after that. Use regex

import re
re.sub('(\.[^.]*)$', '', string)

You are probably encountering a string that doesn't follow your format, and contain no digits.

Try securing your loop by adding a len(x) > 0 indicator:

while len(string) > 0 and not string[-1].isdigit():
     del string[-1]

If you just want to extract the integer from you string, I would suggest regex:

import re
matches = re.findall('\d+\.?\d*', string)
string = int(matches[0]) if len(matches) > 0 else 0

I found the problem... the good old indentation error:

Instead of:

            def valor():
                valor=["valor do contrato:","valor:"]
                for i in valor:
                    if i in lower_extrato:
                        valor=extrato_de_contrato[lower_extrato.rfind(i)+len(i):lower_extrato.find("fonte",lower_extrato.rfind(i))]
                    while valor[-1].isdigit()==False:
                        valor=valor[:-1]

It should go like:

            def valor():
                valor=["valor do contrato:","valor:"]
                for i in valor:
                    if i in lower_extrato:
                        valor=extrato_de_contrato[lower_extrato.rfind(i)+len(i):lower_extrato.find("fonte",lower_extrato.rfind(i))]
                    --> while valor[-1].isdigit()==False:
                    -->     valor=valor[:-1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM