使用Python从JSON解析值

时间:2017-12-07 14:47:28

标签: python json pandas

我希望将以下JSON格式的consumptionSavings值存储为.txt文件。

{
    "_id": "58edf905746de21c401a3dce",
    "sites": [{
        "ecms": [{
            "consumptionSavings": 148,
        "equipmentCost": 3455,
         {
            "energySource": "Electricity",
            "consumptionReduction": {
                "amount": 345435,
                "unit": "MWh"
            },
    "projectDurationMonths": 36
        }
    }

    }
    ]
]
}

我编写了以下代码来提取consumptionSavings的值;

import xlwings as xw
import pandas as pd
import json

data = json.load(open('data.txt'))

# Create a Pandas dataframe from the data.
df = pd.DataFrame({'data':[data["sites"]["ecms"]["consumptionSavings"]]})

wb = xw.Book('Values.xlsx')

ws = wb.sheets['Sheet1']

ws.range('C3').options(index=False).value = df

wb = xw.Book('Result.xlsx')

wb.save()

xw.apps[0].quit()

并返回以下错误:

TypeError: list indices must be integers or slices, not str

我有点困惑。谢谢

0 个答案:

没有答案