type 'exceptions.ValueError': invalid literal for int () with base 10: '19065171.'

0

I have the following code:

for i in range(0, len(terceros_df.NIT)):
    print 'esto es i =', i
    number = terceros_df.iloc[i]['NIT']
    length = len(str(number))
    if length == 10:
        str_number = str(number)
        nit = str_number[:-1]
        dv = str_number[-1:]
        nit = format(int(nit), ',d').replace(',', ".")+ "-" + str(dv)
        terceros_df.loc[i,'NIT'] = nit
        print nit
    else:
        number = format(int(number), ',d').replace(',', ".")
        terceros_df.loc[i,'NIT'] = number
        print number
    print 'La cantidad de caracteres son:',length
    print '-----------------------'

The error is:

 <type 'exceptions.ValueError'>: invalid literal for int() with base 10: '19065171.

According to my understanding, you are trying to read a float, but I am passing an integer, in summary as a result we want an integer not a float. How to solve?

    
asked by Yan Chirino 22.10.2018 в 17:30
source

1 answer

1

You are not passing an integer. You are passing a float. The value that gives you error is '19065171.' . The problem is that it ends with a point.

>>> int('19065171.')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '19065171.'

So if you are going to treat the number as a string, whenever they are numbers with a single decimal , it would be logical to set nit from the penultimate character:

>>> number = 19065171.4
>>> str_number = str(number)
>>> nit = str_number[:-2]
>>> int(nit)
19065171
    
answered by 22.10.2018 в 20:11