从JSON文件中提取数据,keyerror问题

2024-10-03 23:26:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从AWS价格表中提取某些位置,但我一直得到下面的错误代码。JSON结构是否存在错误

Traceback (most recent call last):
  File "/Python/python2.py", line 5, in <module>
    for r in data['SJQU8C5FNVFYMK7X']:
KeyError: 'SJQU8C5FNVFYMK7X

JSON文件

"SJQU8C5FNVFYMK7X" : {
  "sku" : "SJQU8C5FNVFYMK7X",
  "productFamily" : "Compute Instance",
  "attributes" : {
    "servicecode" : "AmazonEC2",
    "location" : "Asia Pacific (Singapore)",
    "locationType" : "AWS Region",
    "instanceType" : "r5n.8xlarge",
    "currentGeneration" : "Yes",
    "instanceFamily" : "Memory optimized",
    "vcpu" : "32",
    "physicalProcessor" : "Intel Xeon Platinum 8259 (Cascade Lake)",
    "clockSpeed" : "2.5 GHz",
    "memory" : "256 GiB",
    "storage" : "EBS only",
    "networkPerformance" : "25 Gigabit",
    "processorArchitecture" : "64-bit",
    "tenancy" : "Shared",
    "operatingSystem" : "Windows",
    "licenseModel" : "No License required",
    "usagetype" : "APS1-BoxUsage:r5n.8xlarge",
    "operation" : "RunInstances:0102",
    "capacitystatus" : "Used",
    "dedicatedEbsThroughput" : "5000 Mbps",
    "ecu" : "NA",
    "enhancedNetworkingSupported" : "No",
    "intelAvxAvailable" : "No",
    "intelAvx2Available" : "No",
    "intelTurboAvailable" : "No",
    "normalizationSizeFactor" : "64",
    "preInstalledSw" : "SQL Ent",
    "servicename" : "Amazon Elastic Compute Cloud"
  }

Python代码:

import json

with open('index (5).json') as json_file:
    data = json.load(json_file)
    for r in data['SJQU8C5FNVFYMK7X']:
        print (r)

你知道我做错了什么吗


Tags: noinawsjsonfordataservice结构
2条回答

在整个过程中缺少了一些花括号,下面的json解析正确

{
    "SJQU8C5FNVFYMK7X": {
        "sku": "SJQU8C5FNVFYMK7X",
        "productFamily": "Compute Instance",
        "attributes": {
            "servicecode": "AmazonEC2",
            "location": "Asia Pacific (Singapore)",
            "locationType": "AWS Region",
            "instanceType": "r5n.8xlarge",
            "currentGeneration": "Yes",
            "instanceFamily": "Memory optimized",
            "vcpu": "32",
            "physicalProcessor": "Intel Xeon Platinum 8259 (Cascade Lake)",
            "clockSpeed": "2.5 GHz",
            "memory": "256 GiB",
            "storage": "EBS only",
            "networkPerformance": "25 Gigabit",
            "processorArchitecture": "64-bit",
            "tenancy": "Shared",
            "operatingSystem": "Windows",
            "licenseModel": "No License required",
            "usagetype": "APS1-BoxUsage:r5n.8xlarge",
            "operation": "RunInstances:0102",
            "capacitystatus": "Used",
            "dedicatedEbsThroughput": "5000 Mbps",
            "ecu": "NA",
            "enhancedNetworkingSupported": "No",
            "intelAvxAvailable": "No",
            "intelAvx2Available": "No",
            "intelTurboAvailable": "No",
            "normalizationSizeFactor": "64",
            "preInstalledSw": "SQL Ent",
            "servicename": "Amazon Elastic Compute Cloud"
        }
    }
}

注意,您可以使用联机linter检查解析错误(例如https://jsonlint.com/

调试的简单方法是打开一个交互式解释器会话,查看出现了哪些键:

>>> import json
>>> json_file = open('index (5).json')
>>> data = json.load(json_file)
>>> data.keys()

如果您想操纵data.keys(),可能需要将其强制转换为列表(即list(data.keys()))。但至少这会告诉你json.load认为键是什么

相关问题 更多 >