简体   繁体   中英

Python Json for-loop

I need to convert a JSON data with one structure to another which I can use to generate NvD3 charts. In the last days I´ve been asking and reading to do this and I achieved a partial solution. It will work unless I add one more key/value to the "data_json" dictionary.

The weirdest thing is that despite that the lines refering to "datos_TEU" are commented, it keeps assigning values to that dictionary entry and don´t know why. Even I just uncomment the first loop it will end up with the same data as the other key. So I end up with 2 copies of the same data inserted in 2 differnt keys. Apart from that I keeps repeating data or making "overlaps" in the loops.

For sure I´m making a big noob error but I can´t see it. I have been printing everything to the shell trying different things all morning and afternoon but I can´t catch the bug.

data = json.load(url_obj)
tarifas = ('2.0A','2.0DHA','2.0DHSA')
fecha = '12345' #Just to develop
terminos = ('Dia','Hora','GEN','NOC','VHC','COFGEN','COFNOC','COFVHC','PMHGEN','PMHNOC','PMHVHC','SAHGEN','SAHNOC','SAHVHC','FOMGEN','FOMNOC','FOMVHC','FOSGEN','FOSNOC','FOSVHC','INTGEN','INTNOC','INTVHC','PCAPGEN','PCAPNOC','PCAPVHC','TEUGEN','TEUNOC','TEUVHC')


data_json = {'datos_TOT':[],'datos_TEU':[],'Fecha':fecha}

for i,tarifa in enumerate(tarifas):
    tarifas_dicc= {'tarifa':tarifa}
    tarifas_dicc['data'] = [] #Clean and create a new empty one.
    data_json['datos_TOT'].append(tarifas_dicc)
=>  #data_json['datos_TEU'].append(tarifas_dicc) I found that problems start when I uncomment this line. From here on the returned data will have duplicities or data that should´nt be there.
    list_terminos = terminos[(2+i)::3] #The original data is coded in a single dictionary and I have to split it into 3 different categories.


    for j in range (0,3):
        periodo_dicc = {'periodo':'{0}-{1}'.format(j,j+1)}
        periodo_dicc['data'] = [] #Clean and create a new empty one.
        #data_json['datos_TEU'][i]['data'].append(periodo_dicc)
        data_json['datos_TOT'][i]['data'].append(periodo_dicc)

        for k,termino in enumerate(list_terminos):
            data_dicc_TOT = {'value':data["PVPC"][j][termino]} #This structure come from the original data_json i´m using

            data_dicc_TOT['label'] = termino
            #data_dicc_TEU = {'value':data["PVPC"][j][list_terminos[0]]}
            #data_dicc_TEU['label'] = list_terminos[0]
            #data_json['datos_TEU'][i]['data'][j]['data'].append(data_dicc_TEU)
            data_json['datos_TOT'][i]['data'][j]['data'].append(data_dicc_TOT)

Where am I assigning the data to the other key?

If you assign tarifas_dicc to both datos_TOT and datos_TEU , both dicts get a reference to the same thing. So when you modify one, the other will see the changes.

Just make sure you create two separate dicts to start with. You can simplify that part of the code as well:

for i,tarifa in enumerate(tarifas):
    data_json['datos_TOT'].append({'tarifa':tarifa, 'data': []}
    data_json['datos_TEU'].append({'tarifa':tarifa, 'data': []}

Also note the same will be true of periodo_dicc later on.

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