Python 如何使用循环从JSON中提取多个数据以创建CSV
如何使用for从JSON中提取Python 如何使用循环从JSON中提取多个数据以创建CSV,python,json,csv,Python,Json,Csv,如何使用for从JSON中提取名称和值,并在CSV中输入所有数据 我希望从我的代码中得到CSV中的名称和值列表。比如: 守则: if valueselector == 'PRINT': idMag = json_dict['data']['archivingLegal']['idMag'] roomid = json_dict['data']['archivingLegal']['roomid'] safeid
名称
和值
,并在CSV中输入所有数据
我希望从我的代码中得到CSV中的名称和值列表。比如:
守则:
if valueselector == 'PRINT':
idMag = json_dict['data']['archivingLegal']['idMag']
roomid = json_dict['data']['archivingLegal']['roomid']
safeid = json_dict['data']['archivingLegal']['safeid']
serieID = json_dict['data']['archivingLegal']['serieID']
serieCode = json_dict['data']['archivingLegal']['serieCode']
fdr = json_dict['data']['archivingLegal']['fdr']
createSafe = json_dict['data']['archivingLegal']['createSafe']
fromGif = json_dict['data']['archivingLegal']['fromGif']
indexFileName = json_dict['data']['archivingLegal']['indexFileName']
originalName = json_dict['data']['archivingLegal']['originalName']
xmlns = json_dict['data']['archivingLegal']['archives']['-xmlns']
for i in range(0, 8):
indexName = json_dict['data']['archivingLegal']['archives']['archive']['index'][i]['name']
indexValue = json_dict['data']['archivingLegal']['archives']['archive']['index'][i]['value']
recordId = json_dict['data']['archivingLegal']['dataMode']['recordId']
raw_data_archivingLegal = [idMag, roomid, safeid,
serieID, serieCode, fdr,
createSafe, fromGif, indexFileName,
originalName, xmlns, indexName,
indexValue, recordId, '',
'', '', '',
'', '', '']
数据帧配置的我的索引:
"----": ['idMag', 'roomid', 'safeid',
'serieID', 'serieCode', 'fdr',
'createSafe', 'fromGif', 'indexFileName',
'originalName', 'xmlns', 'indexName',
'indexValue', 'recordId', '',
'', '', '',
'', '', ''],
"archivingLegal": raw_data_archivingLegal,
JSON:
"data": {
"archivingLegal": {
"archives": {
"-xmlns": "google.com",
"archive": {
"index": [{
"name": "FILENAME",
"value": " "
}, {
"name": "TYPEDOC",
"value": " "
}, {
"name": "ORGANISATION",
"value": " "
}, {
"name": "ACTIVITE",
"value": " "
}, {
"name": "DOC_CODE",
"value": " "
}, {
"name": "CLIENT_CODE",
"value": " "
}, {
"name": "DATE_TRAITEMENT",
"value": " "
}, {
"name": "DUA",
"value": " "
}
]
}
},
"createSafe": " ",
"dataMode": {
"recordId": " "
},
"fdr": " ",
"fromGif": " ",
"idMag": " ",
"indexFileName": " ",
"originalName": " ",
"roomid": " ",
"safeid": " ",
"serieCode": " ",
"serieID": " "
}
我的实际输出csv:
将它们迭代到列表中。然后,您可以保留为列表,或将值合并为单个字符串(如预期输出所示: json数据
json_dict = {"data": {
"archivingLegal": {
"archives": {
"-xmlns": "google.com",
"archive": {
"index": [{
"name": "FILENAME",
"value": " "
}, {
"name": "TYPEDOC",
"value": " "
}, {
"name": "ORGANISATION",
"value": " "
}, {
"name": "ACTIVITE",
"value": " "
}, {
"name": "DOC_CODE",
"value": " "
}, {
"name": "CLIENT_CODE",
"value": " "
}, {
"name": "DATE_TRAITEMENT",
"value": " "
}, {
"name": "DUA",
"value": " "
}
]
}
},
"createSafe": " ",
"dataMode": {
"recordId": " "
},
"fdr": " ",
"fromGif": " ",
"idMag": " ",
"indexFileName": " ",
"originalName": " ",
"roomid": " ",
"safeid": " ",
"serieCode": " ",
"serieID": " "
}}}
代码修改
idMag = json_dict['data']['archivingLegal']['idMag']
roomid = json_dict['data']['archivingLegal']['roomid']
safeid = json_dict['data']['archivingLegal']['safeid']
serieID = json_dict['data']['archivingLegal']['serieID']
serieCode = json_dict['data']['archivingLegal']['serieCode']
fdr = json_dict['data']['archivingLegal']['fdr']
createSafe = json_dict['data']['archivingLegal']['createSafe']
fromGif = json_dict['data']['archivingLegal']['fromGif']
indexFileName = json_dict['data']['archivingLegal']['indexFileName']
originalName = json_dict['data']['archivingLegal']['originalName']
xmlns = json_dict['data']['archivingLegal']['archives']['-xmlns']
############## HERE's THE MODIFICATION ###############
indexName = [ i['name'] for i in json_dict['data']['archivingLegal']['archives']['archive']['index'] ]
indexValue = [ i['value'] for i in json_dict['data']['archivingLegal']['archives']['archive']['index'] ]
indexName = ', '.join(indexName)
indexValue = ', '.join(indexValue)
######################################################
recordId = json_dict['data']['archivingLegal']['dataMode']['recordId']
raw_data_archivingLegal = [idMag, roomid, safeid, serieID, serieCode, fdr,
createSafe, fromGif, indexFileName, originalName,
xmlns, indexName, indexValue, recordId, '', '', '', '', '', '', '']
不清楚您希望预期的输出是什么样子的。@chitown88它现在已被编辑