Python代码KeyError尝试从JSON文件读取

x0fgdtte  于 2023-04-04  发布在  Python
关注(0)|答案(1)|浏览(187)

你好,我有一个Python代码,试图从JSON文件中读取一些元素。这段代码处理一些文件形成一个JSON文件:

with open('C:/asg/response2.json') as json_file:
    data2 = json.load(json_file)

if 'priceResponse' in data2:    
    item = data2['priceResponse']['PriceAvailabilityList'][0]
    property_valuesa.append(item['totalQuantity'])
    property_values2a.append(item['PriceAvailabilityList']['price'])
    
print (property_valuesa)
input ("key enter")

我收到的错误是:

property_valuesa.append(item['totalQuantity'])
KeyError: 'totalQuantity'

需要在代码中更改什么来修复错误,并读取“totalQuantity”元素?对于值的顺序,我尝试了,但没有结果。
这是我正在阅读的JSON文件的一个例子:

{
    "priceResponse": {
        "customerNo": "2312312",
        "userName": "23131@er.com",
        "PriceAvailabilityList": [
            {
                "sku": "12133223",
                "mfgPN": "DELL-sdad",
                "status": "Not authorized to buy",
                "GlobalProductStatusCode": "Not authorized to buy",
                "lineNumber": "1"
            },
            {
                "sku": "38384774",
                "mfgPN": "DELL-E322323H",
                "mfgCode": "23232",
                "status": "Active",
                "description": "27 MON 323232",
                "GlobalProductStatusCode": "Active",
                "price": "195.72",
                "totalQuantity": "138",
                "AvailabilityByWarehouse": [
                    {
                        "warehouseInfo": {
                            "number": "123",
                            "zipcode": "23344",
                            "city": "test, TT",
                            "addr": "testaddress"
                        },
                        "qty": "0"
                    },
                    {
                        "warehouseInfo": {
                            "number": "123",
                            "zipcode": "2344",
                            "city": "TEST, TT",
                            "addr": "testaddress Drive"
                        },
                        "qty": "0"
                    },
                    {
                        "warehouseInfo": {
                            "number": "233",
                            "zipcode": "213",
                            "city": "TEST, te",
                            "addr": "testaddress Pkwy"
                        },
                        "qty": "138"
                    },
                    {
                        "warehouseInfo": {
                            "number": "243",
                            "zipcode": "34324",
                            "city": "test, tets",
                            "addr": "test Way"
                        },
                        "qty": "0"
                    },
                    {
                        "warehouseInfo": {
                            "number": "2312",
                            "zipcode": "2312",
                            "city": "test, TS",
                            "addr": "test"
                        },
                        "qty": "0"
                    }
                ],
                "lineNumber": "1"
            }
        ]
    }
}
8zzbczxx

8zzbczxx1#

PriceAvailabilityList是一个包含多个字典的列表。出现KeyError是因为代码期望totalQuantity是第一个字典中的键:

item = data2['priceResponse']['PriceAvailabilityList'][0]
property_valuesa.append(item['totalQuantity'])

totalQuantityPriceAvailabilityList中 * 第二个 * 字典中的键。

相关问题