Python code KeyError trying to read from a JSON file

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

Python code KeyError trying to read from a JSON file

问题

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

  1. property_valuesa.append(item['totalQuantity'])

更改为:

  1. 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:

  1. with open('C:/asg/response2.json') as json_file:
  2. data2 = json.load(json_file)
  3. if 'priceResponse' in data2:
  4. item = data2['priceResponse']['PriceAvailabilityList'][0]
  5. property_valuesa.append(item['totalQuantity'])
  6. property_values2a.append(item['PriceAvailabilityList']['price'])
  7. print (property_valuesa)
  8. input ("key enter")

The error I receive is:

  1. property_valuesa.append(item['totalQuantity'])
  2. 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:

  1. {
  2. "priceResponse": {
  3. "customerNo": "2312312",
  4. "userName": "23131@er.com",
  5. "PriceAvailabilityList": [
  6. {
  7. "sku": "12133223",
  8. "mfgPN": "DELL-sdad",
  9. "status": "Not authorized to buy",
  10. "GlobalProductStatusCode": "Not authorized to buy",
  11. "lineNumber": "1"
  12. },
  13. {
  14. "sku": "38384774",
  15. "mfgPN": "DELL-E322323H",
  16. "mfgCode": "23232",
  17. "status": "Active",
  18. "description": "27 MON 323232",
  19. "GlobalProductStatusCode": "Active",
  20. "price": "195.72",
  21. "totalQuantity": "138",
  22. "AvailabilityByWarehouse": [
  23. {
  24. "warehouseInfo": {
  25. "number": "123",
  26. "zipcode": "23344",
  27. "city": "test, TT",
  28. "addr": "testaddress"
  29. },
  30. "qty": "0"
  31. },
  32. {
  33. "warehouseInfo": {
  34. "number": "123",
  35. "zipcode": "2344",
  36. "city": "TEST, TT",
  37. "addr": "testaddress Drive"
  38. },
  39. "qty": "0"
  40. },
  41. {
  42. "warehouseInfo": {
  43. "number": "233",
  44. "zipcode": "213",
  45. "city": "TEST, te",
  46. "addr": "testaddress Pkwy"
  47. },
  48. "qty": "138"
  49. },
  50. {
  51. "warehouseInfo": {
  52. "number": "243",
  53. "zipcode": "34324",
  54. "city": "test, tets",
  55. "addr": "test Way"
  56. },
  57. "qty": "0"
  58. },
  59. {
  60. "warehouseInfo": {
  61. "number": "2312",
  62. "zipcode": "2312",
  63. "city": "test, TS",
  64. "addr": "test"
  65. },
  66. "qty": "0"
  67. }
  68. ],
  69. "lineNumber": "1"
  70. }
  71. ]
  72. }
  73. }

答案1

得分: 0

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

  1. item = data2['priceResponse']['PriceAvailabilityList'][0]
  2. 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:

  1. item = data2['priceResponse']['PriceAvailabilityList'][0]
  2. 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:

确定