简体   繁体   English

Python Json for循环

[英]Python Json for-loop

I need to convert a JSON data with one structure to another which I can use to generate NvD3 charts. 我需要将一种结构的JSON数据转换为另一种结构,以用于生成NvD3图表。 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. 除非我在“ data_json”字典中再添加一个键/值,否则它将起作用。

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. 最奇怪的是,尽管引用“ datos_TEU”的行被注释了,但它一直为该字典条目分配值,却不知道为什么。 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. 因此,我最终在2个不同的键中插入了2个相同数据的副本。 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. 当然,我犯了一个很大的noob错误,但我看不到它。 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. 如果将tarifas_dicc都分配给datos_TOTdatos_TEU ,则两个字典都将引用同一事物。 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. 还要注意,以后的periodo_dicc也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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