Tengo un archivo JSON y me gustaria extraer pocos datos para crear un archivo csv como el siguiente:
Serial Number,Province,Name of candidate,Address of the contestant,Symbol Alloted,Party Affiliation,Constituency Number (ID),Constituency Name
,Chaouia - Ouardigha,,,,PI,,Ahlaf
,Chaouia - Ouardigha,,,,PJD,,Ahlaf
,Chaouia - Ouardigha,,,,PAM,,Ahlaf
...
Es decir, para cada circunscripción, los partidos que están allí y la provincia en cada columna.
Aqui esta el archivo JSON relacionado:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"id": "fd597jf1799.1",
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
-7.27163887,
33.24041367
],
[
-7.27163887,
33.24041367
]
]
]
]
},
"geometry_name": "geom",
"properties": {
"id_0": 152,
"iso": "MAR",
"name_0": "Morocco",
"id_1": 1,
"name_1": "Chaouia - Ouardigha",
"id_2": 1,
"name_2": "Ben Slimane",
"id_3": 1,
"name_3": "Ben Slimane",
"id_4": 1,
"name_4": "Ahlaf",
"varname_4": null,
"ccn_4": 0,
"cca_4": null,
"type_4": "Commune Rural",
"engtype_4": "Rural Commune",
"bbox": [
-7.27286911,
33.22112656,
-6.93353081,
33.38970184
],
"swing_count": 1,
"polling_station_count": 15,
"turnout": 0.4780299144225693,
"results": {
"PI": 187,
"PJD": 88,
"PAM": 59,
"USFP": 1530,
"APFGD": 2,
"PPS": 15,
"RNI": 708,
"MP": 56,
"UC": 3,
"FFD": 0,
"MDS": 0,
"AAR": 0,
"P Neo-Democrates": 8,
"PEDD": 0,
"PRD": 2,
"PRV": 0,
"PDI": 0,
"PGVM": 0,
"PALAMAL": 0,
"PCS": 0,
"PUD": 0,
"PDN": 1,
"PLJS": 0,
"PSD": 0,
"P Annahda": 0,
"PA": 0,
"UMD": 0,
"USAPMD": 10
},
"voter_file": {
"nbre_sieges": 3,
"nbre_inscrits": 5953,
"nbre_votants": 2997,
"nbre_nuls": 328,
"nbre_exprimees": 2669
},
"swing_ratio": 0.06666666666666667
}
},
{"type": "Feature",
"id": "fd597jf1799.5",
"geometry": {
"type": "MultiPolygon",
"coordinates": [[[[-7.10879707, 33.63715363], [-7.10618496, 33.6385231]]]]},
"geometry_name": "geom",
"properties": {
"id_0": 152,
"iso": "MAR",
"name_0": "Morocco",
"id_1": 1,
"name_1": "Chaouia - Ouardigha",
"id_2": 1,
"name_2": "Ben Slimane",
"id_3": 1,
"name_3": "Ben Slimane",
"id_4": 5,
"name_4": "Moualine El Ghaba",
"varname_4": None,
"ccn_4": 0,
"cca_4": None,
"type_4": "Commune Rural",
"engtype_4": "Rural Commune",
"bbox": [-7.25548506, 33.53932571, -7.01937389, 33.7419014],
"swing_ratio": 0.0
}
}
]
}
Es por decir me gustaria, para cada candidates en results
, crear:
Serial Number,Province,Name of candidate,Address of the contestant,Symbol Alloted,Party Affiliation,Constituency Number (ID),Constituency Name
,{{name_1}},,,,{{results}}[i],,{{name_4}}
...
Como pueden ver, algunos campos de resultados pueden no existir en una circunscripción determinada. Así que ya estoy intentando hacer pruebas cuando están allí.
>>> for i in range(0,len(dct['features'])):
... if(dct['features'][i]['properties']['results']):
... for key in dct['features'][i]['properties']['results'].keys():
... print(key)
Pero me da:
...
USAPMD
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
KeyError: 'results'
Estoy abierto a respuestas en javascript tambien