首页 > 解决方案 > 如何使用循环从 JSON 中提取多个数据以创建 CSV

问题描述

如何使用 for 从我的 JSON 中提取namevalue输入我的所有数据并在我的 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, '',
                                      '', '', '',
                                      '', '', '']

我的 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 :

在此处输入图像描述

标签: pythonjsoncsv

解决方案


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

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

推荐阅读