Skip to content Skip to sidebar Skip to footer

Python Error: Typeerror: List Indices Must Be Integers Or Slices, Not Str

i get this error in this line : valor_mensal_aux[i] = int(data['Dados'][0][json_date][0]['valor']) i tried to post some code here so you guys can get context for all the variables:

Solution 1:

I tried accessing the web page concerned, and a typical object loaded from the JSON response looks like this:

[{'Dados': {'201101': [{'dim_3': 'T',
                        'dim_3_t': 'Total',
                        'geocod': '1111609',
                        'geodsg': 'Viana do Castelo',
                        'valor': '779'}]},
  'DataExtracao': '2020-09-03T14:21:27.691+01:00',
  'DataUltimoAtualizacao': '2020-08-27',
  'IndicadorCod': '0010042',
  'IndicadorDsg': 'Valor mediano de avaliação bancária (€/ m²) por Localização ''geográfica (Município - 2013) e Tipo de construção; Mensal ''- INE, Inquérito à avaliação bancária na habitação',
  'MetaInfUrl': 'https://www.ine.pt/bddXplorer/htdocs/minfo.jsp?var_cd=0010042&lingua=PT',
  'UltimoPref': 'Julho de 2020'}]

This means that you need to access it like this:

data[0]['Dados'][json_date][0]['valor']

You had instead:

data['Dados'][0][json_date][0]['valor']

You will also have issues with trying to assign off the end of the list. You probably want something like this:

       for year_code in year_codes:
           valor_mensal_aux = []
           for month_code in month_codes:
                ......
                valor_mensal_aux.append(int(data[0]['Dados'][json_date][0]['valor']))

and do not loop over i inside your month loop - only append it once for each month.

Post a Comment for "Python Error: Typeerror: List Indices Must Be Integers Or Slices, Not Str"