{已解决}尝试从 json 中提取数据并使用数据创建 excel 文件

问题描述 投票:0回答:0

我正在尝试编写一个 python 脚本来:

  1. 抓取一个 JSON 文件。
  2. 从 json 文件中提取特定的标头。 (想不通)
  3. 在 excel 文件中创建标题作为列。
  4. 通过json解析,将数据放到合适的列中。

我很新,正在努力让它工作。

import json
import xlwt
# Workbook is created
wb = xlwt.Workbook()
# add_sheet is used to create sheet.
sheet1 = wb.add_sheet('Sheet 1')
# open json file and store it in data variable
with open('input.json') as f:
    data = json.load(f)
# write the column names in the excel sheet
sheet1.write(0, 0, 'Name')
sheet1.write(0, 1, 'Schedule')
sheet1.write(0, 2, 'Time Window')
sheet1.write(0, 3, 'Type')
sheet1.write(0, 4, 'Tiers')
# iterate over the data and write it to the excel file
row = 1;
for item in data:
#write each value to the corresponding column
    sheet1.write(row, 0, item['Name'])
    sheet1.write(row, 1, item['Schedule'])
    sheet1.write(row, 2, item['Time Window'])
    sheet1.write(row, 3, item['Type'])
    sheet1.write(row, 4, item['Tiers'])
    row += 1;
#save the file
wb.save('data_converted_to_excel.xls')

JSON 示例

{
  "name": "PLACEHOLDER",
  "description": "Health Rule Export",
  "version": 1,
  "healthRuleMembers": [
    {
      "model": {
        "id": 123,
        "name": "PLACEHOLDER",
        "enabled": true,
        "useDataFromLastNMinutes": 30,
        "waitTimeAfterViolation": 30,
        "scheduleName": "Always",
        "affects": {
          "affectedEntityType": "TIER_NODE_HARDWARE",
          "affectedEntities": {
            "tierOrNode": "NODE_AFFECTED_ENTITIES",
            "typeofNode": "DOT_NET_NODES",
            "affectedNodes": {
              "affectedNodeScope": "ALL_NODES"
            }
          }
        },
        "evalCriterias": {
          "criticalCriteria": {
            "conditionAggregationType": "ALL",
            "conditionExpression": null,
            "conditions": [
              {
                "name": "PLACEHOLDER",
                "shortName": null,
                "evaluateToTrueOnNoData": false,
                "evalDetail": {
                  "evalDetailType": "SINGLE_METRIC",
                  "metricAggregateFunction": "VALUE",
                  "metricPath": "PLACEHOLDER",
                  "metricEvalDetail": {
                    "metricEvalDetailType": "SPECIFIC_TYPE",
                    "compareCondition": "GREATER_THAN_SPECIFIC_VALUE",
                    "compareValue": 75
                  }
                },
                "triggerEnabled": false,
                "minimumTriggers": 0
              }
            ],
            "evalMatchingCriteria": null
          },
          "warningCriteria": {
            "conditionAggregationType": "ALL",
            "conditionExpression": null,
            "conditions": [
              {
                "name": "PLACEHOLDER",
                "shortName": null,
                "evaluateToTrueOnNoData": false,
                "evalDetail": {
                  "evalDetailType": "SINGLE_METRIC",
                  "metricAggregateFunction": "VALUE",
                  "metricPath": "PLACEHOLDER",
                  "metricEvalDetail": {
                    "metricEvalDetailType": "SPECIFIC_TYPE",
                    "compareCondition": "GREATER_THAN_SPECIFIC_VALUE",
                    "compareValue": 50
                  }
                },
                "triggerEnabled": false,
                "minimumTriggers": 0
              }
            ],
            "evalMatchingCriteria": null
          }
        }
      },
      "memberType": "HEALTH_RULE"
    },

我试过用我朋友说的用 项目 = json.loads(项目) 但一直无法让它工作。

我是初学者。对不起!

python json excel scripting xlwt
© www.soinside.com 2019 - 2024. All rights reserved.