Python 属性错误:';非类型';对象没有属性';下';

Python 属性错误:';非类型';对象没有属性';下';,python,if-statement,Python,If Statement,我试图运行python文件,但出现以下错误: File "/home/hadi/Software/tensorflow/TEST_FRCN_ROOT/tools/../lib/datasets/pascal_voc.py", line 212, in _load_pascal_annotation cls = self._class_to_ind[obj.find('name').text.lower().strip()] AttributeError: 'NoneType' obj

我试图运行python文件,但出现以下错误:

  File "/home/hadi/Software/tensorflow/TEST_FRCN_ROOT/tools/../lib/datasets/pascal_voc.py", line 212, in _load_pascal_annotation
    cls = self._class_to_ind[obj.find('name').text.lower().strip()]
AttributeError: 'NoneType' object has no attribute 'lower'
这是导致错误的代码部分:

%%  Load object bounding boxes into a data frame.
        for ix, obj in enumerate(objs):
            bbox = obj.find('bndbox')
            # Make pixel indexes 0-based
            x1 = float(bbox.find('xmin').text) - 1
            y1 = float(bbox.find('ymin').text) - 1
            x2 = float(bbox.find('xmax').text) - 1
            y2 = float(bbox.find('ymax').text) - 1
            cls = self._class_to_ind[obj.find('name').text.lower().strip()]
            boxes[ix, :] = [x1, y1, x2, y2]
            gt_classes[ix] = cls
            overlaps[ix, cls] = 1.0
            seg_areas[ix] = (x2 - x1 + 1) * (y2 - y1 + 1)

我可以在这里添加一个条件来处理任何none对象吗?

可以这样使用

for ix, obj in enumerate(objs):
            bbox = obj.find('bndbox')
            # Make pixel indexes 0-based
            x1 = float(bbox.find('xmin').text) - 1
            y1 = float(bbox.find('ymin').text) - 1
            x2 = float(bbox.find('xmax').text) - 1
            y2 = float(bbox.find('ymax').text) - 1
            if obj.find('name').text != None:
              cls = self._class_to_ind[obj.find('name').text.lower().strip()]
              boxes[ix, :] = [x1, y1, x2, y2]
              gt_classes[ix] = cls
              overlaps[ix, cls] = 1.0
              seg_areas[ix] = (x2 - x1 + 1) * (y2 - y1 + 1)

那么,如果在
obj.find('name')
中没有文本,该怎么办呢?当然可以添加条件,但如果条件不匹配怎么办?那么我必须检查每个文件以查看是否有缺少name属性的对象?大约有1200个文件。还有其他方法吗?这不是我说的,你也没有回答我的问题。事实上,这根本不应该发生,因为我确定所有对象都有一个带有文本的名称标签。但是你在这里有一个错误,证明不是这样。如果cls没有,请使用
,无需使用慢速选项。根据回溯,
.text
属性是
None
,而不是
find()
的返回值。
.text
None
,更新了代码我应该在if前面放置什么而不是
cls
。@hadigharemannezhad使用更新的代码,试着看看它是否有效