TypeError: 'int' object has no attribute '__getitem__'

0

I have a data frame with different identification numbers and I am trying to format it in two sections, if it has more than 10 digits the format is: 123.456.789-0, if it is different to 10 digits (less or more) the format is : 123,456,789 / 123,456,789,000

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

The error message is:

TypeError: 'int' object has no attribute '__getitem__'

::::::::::: Attached reference image :::::::::::::

    
asked by Yan Chirino 20.10.2018 в 01:25
source

1 answer

1

When you define number = int(mydataset_df.iloc[i]['NIT']) you have an integer number. In the next line, what you do with length = len(str(number)) is to get the length of the string composed of number . However, notice that at no time do you keep that chain anywhere. Therefore you have a number of integer type and a length of integer type, no string.

As a solution, before nit = number[:-1] you should convert the number into a chain. For example, str_number = str(number) .

    
answered by 20.10.2018 / 01:31
source