【问题标题】:Build dictionary from JSON and export it to Pandas从 JSON 构建字典并将其导出到 Pandas
【发布时间】:2021-11-25 23:21:55
【问题描述】:

我正在尝试构建一个从下面的字典中提取离线端点的脚本:

[
    {
        "name": "My AP",
        "serial": "Q234-ABCD-5678",
        "mac": "00:11:22:33:44:55",
        "status": "online",
        "lanIp": "1.2.3.4",
        "publicIp": "123.123.123.1",
        "networkId": "N_24329156"
    }
]

然后填充字典并使用 pandas 将输出导出到 xlsx

# Build dictionary to organize endpoints
endpoint = {'name' : [], 'serial' : [], 'mac' : [], 'publicIp' : [], 'networkId' : [], 'status' : [],'lastReportedAt' : [], 'usingCellularFailover' : [], 'wan1Ip' : [], 'wan2Ip' : [], 'lanIp' : []}                                   
                                        


# Iterate over the endpoints to fill dictionary
for i in range(len(response_data)):
    if response_data[i]['status'] == 'offline':
        endpoint['Name'].append(['name'])
        endpoint['Serial'].append(['serial'])
        endpoint['MAC'].append(['mac'])
        endpoint['Public IP'].append(['publicIp'])
        endpoint['Network ID'].append(['networkId'])
        endpoint['Status'].append(['status'])
        endpoint['Last Reied'].append(['lastReiedAt'])
        endpoint['Cellular'].append(['usingCellularFailover'])
        endpoint['WAN 1'].append(['wan1Ip'])
        endpoint['WAN 2'].append(['wan2Ip'])
        endpoint['LAN'].append(['lanIp'])
        df = pd.DataFrame.from_dict(endpoint)
        df.to_excel("output.xlsx", index=False)  

我很确定有一种更有效的方法来完成任务,比如将输出导入 pandas 并对数据进行排序,但我仍然是菜鸟

【问题讨论】:

  • 试试DataFrame.from_json()
  • 你应该在for-loop 之后创建dataframe - 这样你只会做一次,你只会写一次文件。
  • 你可以先学会使用for item in response_data:,然后再使用item['status']item['name']等。

标签: python json devops meraki-api


【解决方案1】:

您可以将字典列表直接转换为Pandas dataframe

【讨论】:

    【解决方案2】:

    如果您的字典列表称为“response_data”,那么您可以像这样直接将该列表转换为 DataFrame:

    df = pd.DataFrame(response_data, index=range(len(response_data)))
    df.to_excel("output.xlsx", index=False) 
    

    【讨论】:

      【解决方案3】:

      您可以直接使用DataFrame 以及以后重命名列和过滤数据。

      response_data = [
          {
              "name": "My AP",
              "serial": "Q234-ABCD-5678",
              "mac": "00:11:22:33:44:55",
              "status": "online",
              "lanIp": "1.2.3.4",
              "publicIp": "123.123.123.1",
              "networkId": "N_24329156"
          },
      
          {
              "name": "My AP",
              "serial": "Q234-ABCD-5678",
              "mac": "00:11:22:33:44:55",
              "status": "offline", 
              "lanIp": "1.2.3.4",
              "publicIp": "123.123.123.1",
              "networkId": "N_24329156"
          }
      ]
      
      import pandas as pd
      
      df = pd.DataFrame(response_data)
      
      df = df.rename(columns={
          'name': 'Name',
          'serial': 'Serial',
          'mac': 'MAC',
          'status': 'Status',
          'publicIp': 'Public IP',
          'networkId': 'Network ID',
          'lastReiedAt': 'Last Reied',
          'usingCellularFailover': 'Cellular',
          'wan1Ip': 'WAN 1',
          'wan2Ip': 'WAN 2',
          'lanIp': 'LAN',
      })
      
      df = df[ df['Status'] != 'offline' ]
      
      print(df)
      
      df.to_excel("output.xlsx", index=False) 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-21
        • 2021-11-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-25
        • 1970-01-01
        • 1970-01-01
        • 2021-10-26
        相关资源
        最近更新 更多