I tried to create a csv file from two json files.
First json file :
{ "attributes": [
{
"code": "ean",
"description": "the ean",
"example": null,
"hierarchy_code": null,
"label": "ean",
"required": true,
"type": "TEXT",
"type_parameter": null,
"values": null,
"values_list": "some value"
},
...
Second json file :
{
"code": "the code",
"label": "shoes",
"values": [
{
"code": "COL_100",
"label": "white"
},
{
"code": "COL_101",
"label": "blue"
},
...
I need to get the value from the key "values_list" in the first json. With that key I can get a list from second json (which return a random number of values) and take the "label" key value.
What I’ve found is to make two loop like these :
for att in first_json['attributes']:
csv_dict[att['values_list']] = []
for val in second_json['values']:
csv_dict[att['values_list']].append(val['label'])
Which creates a dictionary like these:
{'label1': [val1, val2, val3],
'label2': [otherval1, otherval2, ...],
...}
My question :
With my "csv_dict" dictionary, how can I create a csv structured like this:
label1 | label2 | ... | labeln
val1 | otherval1 | ... | valn
val2 | otherval2 | ... |
val3 | ... | ... |
My actual dictionary not seems to fit with the usual use of csv.Dictwriter() method.
https://docs.python.org/2/library/csv.html#csv.DictWriter
I try to use the zip function like this :
for elem in zip(*labels.values()):
data.append(";".join([otherelm for otherelem in elem]))
In hope to create an csv file manually but my attempt failed.
Thanks for your help.