Python code KeyError trying to read from a JSON file

huangapple go评论53阅读模式
英文:

Python code KeyError trying to read from a JSON file

问题

你好,你的Python代码尝试从JSON文件中读取一些元素。你遇到的错误是因为"totalQuantity"不在第一个项目中。要修复这个错误,你可以将以下代码行:

property_valuesa.append(item['totalQuantity'])

更改为:

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

这将允许你正确访问"totalQuantity"元素。

英文:

Hello I have a python code to try to read from a JSON file some elements. This code to process some files form a JSON file:

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")

The error I receive is:

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

What is needed to change in code to fix the error and that reads the “totalQuantity” element? For the order of the values is what I tried but I don’t have results.

This is an example of the JSON file I’m reading:

{
	"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"
			}
		]
	}
}

答案1

得分: 0

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

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

然而,totalQuantity 是在 PriceAvailabilityList 中的 第二个 字典中的键。

英文:

PriceAvailabilityList is a list containing a number of dictionaries. The KeyError occurs because the code expects totalQuantity to be a key in the first dictionary:

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

totalQuantity is a key in the second dictionary in PriceAvailabilityList.

huangapple
  • 本文由 发表于 2023年4月1日 00:50:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75900924.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定