I modified the original question since I realized it was not properly presented.
I am working on a project in which I extract some values from a JSON and then pass them to a CSV file. The problem I currently have is that some of those values should be placed individually. However, I, so far, have only been able to placed them in the same group.
This is an example of what I currently have:
id,var_x,var_y,var_z,answer
ABCD, AA , 11 , A1 ,['CH1','CH2','CH3','CH4']
DCBA, BB , 22 , B1 ,['CH5','CH6']
TTTT,
XXXX, CC , 33 , C1, ['CH7','CH8','CH9']
This is what I am trying to get:
id,var_x,var_y,var_z,answer
[ 'ABCD’ , ’AA’ , ’11’ , ’A1’ , ’CH1' ]
[ 'ABCD’ , ’AA’ , ’11’ , ’A1’ , ’CH2’ ]
[ 'ABCD’ , ’AA’ , ’11’ , ’A1’ , ’CH3’ ]
[ 'ABCD’ , ’AA’ , ’11’ , ’A1’ , ’CH4’ ]
[ 'DCBA’ , ’BB , ’22’ , ’B1’ , ’CH5’ ]
[ 'DCBA’ , ’BB’ , ’22’ , ’B1’ , ’CH6’ ]
['TTTT']
[ 'XXXX’ , ’CC’ , ’33’ , ’C1’ , ’CH7’ ]
[ 'XXXX’ , ’CC’ , ’33’ , ’C1’ , ’CH8’ ]
[ 'XXXX’ , ’CC’ , ’33’ , ’C1’ , ’CH9’ ]
As you can see the value of 'id',var_x ,var_y and var_z should repeat depending on the value 'answer'. There are some cases in which the value 'answer' does not have information which is fine.
This is the code I have:
data=response.json()
id, answer, = [], [],
for item in data:
id.append(item.get('id',''))
answer.append(item.get('answer',''))
header = ['id','answer']
with open('stack2.csv','w') as file:
writer = csv.writer(file)
writer.writerow(header)
writer.writerows((id,answer))