【问题标题】:How to resolve the following python Key Error如何解决以下 python 键错误
【发布时间】:2021-09-09 04:13:12
【问题描述】:

我正在遍历 json 文件并尝试在 python 中读取它,但即使文件中存在密钥,它也会显示密钥错误。我正在尝试按如下方式遍历 json 文件

annotations1 = json.load(open('C:/Users/abhin/OneDrive/Desktop/MASK RCNN/DATA SET/train/train_json.json'))  
annotations = list(annotations1.values())  # don't need the dict keys
annotations = [a for a in annotations if a['regions']]

for a in annotations:
    polygons = [r['shape_attributes'] for r in a['regions']] 
    print(a["regions"])
    objects = [s['region_attributes']['names'] for s in a['regions']]
    print("objects:",objects)
    
    name_dict = {"holes": 1}
    num_ids = [name_dict[a] for a in objects]

要重新检查密钥 names 是否存在,我打印 a["regions"] ,您可以在其中看到密钥 names 存在

[{'shape_attributes': {'name': 'ellipse', 'cx': 364, 'cy': 160, 'rx': 9.541, 'ry': 15.19, 'theta': 0.748}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 529, 'cy': 168, 'rx': 10.336, 'ry': 17.991, 'theta': 0.785}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 405, 'cy': 162, 'rx': 9.541, 'ry': 17.438, 'theta': 0.753}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 619, 'cy': 174, 'rx': 11.245, 'ry': 19.878, 'theta': -2.356}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 266, 'cy': 154, 'rx': 7.156, 'ry': 15.19, 'theta': 0.822}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 481, 'cy': 167, 'rx': 9.541, 'ry': 17.438, 'theta': 0.753}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 678, 'cy': 178, 'rx': 12.722, 'ry': 20.365, 'theta': 0.896}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 300, 'cy': 156, 'rx': 7.156, 'ry': 15.19, 'theta': 0.822}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 535, 'cy': 210, 'rx': 7.156, 'ry': 9.574, 'theta': -2.297}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 690, 'cy': 223, 'rx': 7.156, 'ry': 10.697, 'theta': 0.733}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 383, 'cy': 357, 'rx': 6.21, 'ry': 9.541, 'theta': 0.876}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 221, 'cy': 183, 'rx': 3.976, 'ry': 9.541, 'theta': -2.498}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 300, 'cy': 190, 'rx': 5.091, 'ry': 9.541, 'theta': 0.896}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 557, 'cy': 212, 'rx': 7.156, 'ry': 9.706, 'theta': -2.531}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 315, 'cy': 191, 'rx': 4.498, 'ry': 9.541, 'theta': 0.785}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 426, 'cy': 200, 'rx': 5.622, 'ry': 9.541, 'theta': -2.356}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 407, 'cy': 199, 'rx': 5.091, 'ry': 9.541, 'theta': -2.246}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 717, 'cy': 226, 'rx': 7.156, 'ry': 9.574, 'theta': -2.297}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 425, 'cy': 352, 'rx': 4.771, 'ry': 7.951, 'theta': 0.644}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 640, 'cy': 366, 'rx': 7.156, 'ry': 9.574, 'theta': -2.297}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 522, 'cy': 384, 'rx': 7.156, 'ry': 11.301, 'theta': 0.686}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 459, 'cy': 333, 'rx': 7.156, 'ry': 9.706, 'theta': 0.611}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 494, 'cy': 376, 'rx': 5.566, 'ry': 8.996, 'theta': -2.356}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 334, 'cy': 311, 'rx': 7.156, 'ry': 9.574, 'theta': -2.297}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 612, 'cy': 362, 'rx': 8.746, 'ry': 13.68, 'theta': 0.62}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 228, 'cy': 293, 'rx': 5.622, 'ry': 9.541, 'theta': 0.785}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 573, 'cy': 382, 'rx': 7.156, 'ry': 9.574, 'theta': -2.297}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 248, 'cy': 326, 'rx': 4.771, 'ry': 5.622, 'theta': -2.356}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 202, 'cy': 308, 'rx': 4.771, 'ry': 6.41, 'theta': -2.622}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 244, 'cy': 295, 'rx': 5.622, 'ry': 9.541, 'theta': 0.785}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 304, 'cy': 328, 'rx': 4.771, 'ry': 8.452, 'theta': 0.719}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 152, 'cy': 279, 'rx': 4.771, 'ry': 7.951, 'theta': 0.644}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 358, 'cy': 348, 'rx': 4.771, 'ry': 7.951, 'theta': 0.644}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 352, 'cy': 314, 'rx': 4.771, 'ry': 8.452, 'theta': 0.719}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 266, 'cy': 333, 'rx': 5.622, 'ry': 9.541, 'theta': 0.785}, 'region_attributes': {'names': 'holes'}}, {'shape_attributes': {'name': 'ellipse', 'cx': 481, 'cy': 337, 'rx': 7.156, 'ry': 9.574, 'theta': -2.297}, 'region_attributes': {'names': 'holes'}}]

我收到以下错误

Traceback (most recent call last):
  File "custom.py", line 198, in <module>
    train(model)
  File "custom.py", line 165, in train
    dataset_train.load_custom("C:/Users/abhin/OneDrive/Desktop/MASK RCNN/DATA SET", "train")
  File "custom.py", line 97, in load_custom
    objects = [s['region_attributes']['names'] for s in a['regions']]
  File "custom.py", line 97, in <listcomp>
    objects = [s['region_attributes']['names'] for s in a['regions']]
KeyError: 'names'

【问题讨论】:

  • 这只是a["regions"] 之一。也许annotations 中的其他一个没有names
  • @Barmar 打败了我。您正在遍历大量 a 对象,因此其中一个对象一定没有您要查找的内容。使用tryexcept 语句过滤那些抛出KeyError
  • 我一定会检查的。这可能是错误的唯一原因吗?
  • 根据您发布的内容,是的。

标签: python list dictionary key keyerror


【解决方案1】:

似乎有一些未定义的数据集。尝试检查所有数据是否都有各自的类。或者,如果您使用的是 VIA,请尝试检查 def 上的单选按钮,将类设置为默认值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 2021-03-14
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多