【问题标题】:Python how to iterate through all keys and values in nested json to put into a csv filePython如何遍历嵌套json中的所有键和值以放入csv文件
【发布时间】:2019-12-28 18:12:28
【问题描述】:

大家好,我有这个巨大的嵌套 json 响应

{
  "success": true,
  "Result": {
    "IsAggregate": false,
    "Count": 37,
    "Columns": [
      ...
      ...
      ...
      ...
    ],
    "FullCount": 37,
    "Results": [
      {
        "Entities": [
          {
            "Type": "User",
            "Key": "adam",
            "IsForeignKey": true
          }
        ],
        "Row": {
          "PrincipalType": "User",
          "_NumDenyAdd": "None",
          "objecttype": "Row",
          "objectname": "Row|oath|41",
          "EventType": "Cloud.Core.Access.Rights.Change",
          "level": "Info",
          "RequestHostName": "6.1.7.3",
          "Principal": "a-1-4a-ad-4",
          "NumGrantAdd": "GenericAll",
          "NormalizedUser": "adam",
          "_IPaddress": "1.1.10.10",
          "WhenOccurred": "/Date(1577124009000)/",
          "_NumDenyRemove": "None",
          "_NumGrantRemove": "None",
          "_Principalname": null
        }
      },
      {
        "Entities": [
          {
            "Type": "User",
            "Key": "eve",
            "IsForeignKey": true
          }
        ],
        "Row": {
          "PrincipalType": "User",
          "_NumDenyAdd": "None",
          "objecttype": "Row",
          "objectname": "Row|pvcheckout|",
          "EventType": "Cloud.Core.Access.Rights.Change",
          "level": "Info",
          "RequestHostName": "10.100.10.10",
          "Principal": "a1",
          "NumGrantAdd": "GenericAll",
          "NormalizedUser": "eve",
          "_IPaddress": "10.20.20.40.50",
          "WhenOccurred": "/Date(1576771533608)/",
          "_NumDenyRemove": "None",
          "_NumGrantRemove": "None",
          "_Principalname": null
        }
      },
      {
        "Entities": [
          {
            "Type": "User",
            "Key": "SYSTEM$",
            "IsForeignKey": true
          }
        ],
        "Row": {
          "PrincipalType": "User",
          "_NumDenyAdd": "None",
          "objecttype": "File",
          "objectname": "File|/Traces/Cps",
          "EventType": "Cloud.Core.Access.Rights.Change",
          "level": "Info",
          "RequestHostName": "130.100.500.204",
          "Principal": "a1",
          "NumGrantAdd": "Read",
          "NormalizedUser": "SYSTEM$",
          "_IPaddress": "10.81.700.20",
          "WhenOccurred": "/Date(1576771134144)/",
          "_NumDenyRemove": "None",
          "_NumGrantRemove": "None",
          "_Principalname": null
        }
      },
                {
        "Entities": [
          {
            "Type": "User",
            "Key": "john",
            "IsForeignKey": true
          }
        ],
        "Row": {
          "PrincipalType": "User",
          "_NumDenyAdd": "None",
          "objecttype": "Row",
          "objectname": "Row|pvcheckout|e069f223-cb58-4843-ba29-55a00ee1f247",
          "EventType": "Cloud.Core.Access.Rights.Change",
          "level": "Info",
          "RequestHostName": "08.6.3.9",
          "Principal": "a1",
          "NumGrantAdd": "GenericAll",
          "NormalizedUser": "john",
          "_IPaddress": "8.6.3.9",
          "WhenOccurred": "/Date(1575048797174)/",
          "_NumDenyRemove": "None",
          "_NumGrantRemove": "None",
          "_Principalname": null
        }
      }
    ],
    "ReturnID": ""
  },
  "Message": null,
  "MessageID": null,
  "Exception": null,
  "ErrorID": null,
  "ErrorCode": null,
  "IsSoftError": false,
  "InnerExceptions": null
}

我想获取实体键和值的所有出现以及行键和值以将它们放入 csv 文件中。我做了什么

responseObject = r.json() # r is the get request, I store my response into a json
res_data = responseObject['Result']['Results'] # accessing result to reach results where the data i want resides

with open('test_data.csv', 'w') as file1:
    csv.writer = csv.DictWriter(file1,delimiter='|') # error occurs here no fieldname parameter
    for result in res_data:
        csv.writer.writerow(result['Entities']) 
        csv.writer.writerow(result['Row'])

这是我遇到错误和困惑的地方。我收到的第一个错误是没有字段名称参数需要来自“实体”和“行”的键,但我确信还有另一种方法可以解决这个问题。

第二个错误是csv.writer.writerow(),如果我将必填字段写入 csv,它们将相互覆盖。有什么建议或想法来解决这个问题?我知道我遗漏了一些明显的东西

【问题讨论】:

  • 关于你的第一个错误,docs 为 csv 提供字段名,而你不是
  • 关于您的第二个错误,您还有另一个问题,EntitiesRows 不共享相同数量的列,因此如果您将它们写入同一个 csv,则必须将它们连接到先到一个对象
  • @aws_apprentice 感谢您的回复,我阅读了文档,我会在一个数组中列出实体中的键,然后在行中列出另一个?
  • 您必须将列名数组传递给字典编写器,是的,您编写的每一行都应该有相应的列名
  • @aws_apprentice AttributeError: 'list' object has no attribute 'keys' 在这种情况下是否需要 csv 标头?

标签: python json csv


【解决方案1】:

试试这个,代码自己解释:

res_data = data['Result']['Results']
fields = [
    'Type',
    'Key',
    'IsForeignKey',
    "PrincipalType",
    "_NumDenyAdd",
    "objecttype",
    "objectname",
    "EventType",
    "level",
    "RequestHostName",
    "Principal",
    "NumGrantAdd",
    "NormalizedUser",
    "_IPaddress",
    "WhenOccurred",
    "_NumDenyRemove",
    "_NumGrantRemove",
    "_Principalname"
]
with open('test_data.csv', 'w', newline='') as file1:
    csv_writer = csv.writer(file1)
    csv_writer.writerow(fields)
    for user in res_data:
        dataToAppend = []
        for each in fields:
            if len(dataToAppend) < 3:
                dataToAppend.append(str(user['Entities'][0][each]))
            else:
                dataToAppend.append(user['Row'][each])
        print(dataToAppend)
        csv_writer.writerow(dataToAppend)

【讨论】:

    猜你喜欢
    • 2014-09-23
    • 2017-09-07
    • 1970-01-01
    • 2020-04-09
    • 2018-05-07
    • 2018-02-11
    • 2022-11-21
    • 2022-01-01
    相关资源
    最近更新 更多