如何使用循环从JSON提取多个数据以创建CSV

时间:2019-12-20 10:45:12

标签: python json csv

如何使用for从JSON中提取namevalue并将所有数据输入CSV中?

我希望从代码中得到CSV中的名称和值列表。喜欢:

enter image description here

代码:

        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, '',
                                      '', '', '',
                                      '', '', '']

我的DataFrame配置索引:

"----": ['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:

enter image description here

1 个答案:

答案 0 :(得分:1)

将它们迭代到列表中。然后,您可以保留为列表,或将值连接到单个字符串中(如预期输出所示:

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, '',  '', '', '',  '', '', '']