1. 程式人生 > >Python學習筆記:AttributeError: 'NoneType' object has no attribute 'text' 解決

Python學習筆記:AttributeError: 'NoneType' object has no attribute 'text' 解決

#前言
最近在學習python,犯了很多低階錯誤,總結一下
#問題
AttributeError: ‘NoneType’ object has no attribute ‘text’
#出處

difficult = obj.find('difficult').text

方案

錯誤提示的是空元素,沒有對應的屬性。

<object>
    <name>1</name>
    <pose>Unspecified</pose>
    <truncated>0</truncated>
    <Difficult
>
0</Difficult>

仔細檢視可知,原文件中是Difficult而不是difficult。
對應於這類錯誤,以後要更加小心檢查大小寫。