使用 python ElementTree 访问 child 的 child 的属性

Access attribute of child of a child using python ElementTree

xml 文件:

<Protocol1 channel="AljazeeraHD"> 

<frame source="vd01" id="4">

</frame>
<frame id="18" source="vd01">
   <rectangle id="1" height="87" width="976" y="889" x="414"/>   
</frame>
<frame id="23" source="vd01">
   <rectangle id="1" x="300" y="886" width="1095" height="89"/>   
</frame>
<frame id="35" source="vd01">
   <rectangle id="1" x="316" y="53" width="242" height="34"/>   
   <rectangle id="2" x="635" y="886" width="755" height="90"/>
</frame>
</Protocol1>

想要从矩形访问属性 'x' 的值。

要转换的代码 xml_to _csv:

def xml_to_csv(path):

xml_list = []
for xml_file in glob.glob(path):
    tree = ET.parse(xml_file)
    root = tree.getroot()
    for member in root.findall('frame'):
        frameNo = root.attrib['channel']+'_'+member.attrib['source']+'_frame_'+member.attrib['id']
        #print(member.find('rectangle').attrib['x'])
        #print(member.getchildren().attrib['x'])
        #print(member.find('rectangle').attrib['x'])
        value = (frameNo,
                 1920,
                 1080,
                 'Artificial',
                 int(member.find('rectangle').attrib['x']),                    
                 )
        xml_list.append(value)
print(glob.glob(path))
column_name = ['FrameNo', 'imgwidth', 'imgheight',
               'class','X']
xml_df = pd.DataFrame(xml_list, columns=column_name)
print(xml_list)        
return xml_df

其中 'member= frame' 在一个循环中。

1.

member.getchildren().attrib['x'] 

AttributeError: 'NoneType' object 没有属性 'attrib'

2.

member.find('rectangle').attrib['x']

AttributeError: 'list' object 没有属性 'attrib'

由于您正在使用 ElementTree 并假设您的代码如下所示:

members = """
<doc>
   <frame id="18" source="vd01">
      <rectangle id="1" height="87" width="976" y="889" x="414"/>
   </frame>
   <frame id="19" source="vd01">
      <rectangle id="1" height="87" width="976" y="889" x="333"/>
   </frame>
</doc>
"""

那么这应该有效:

import xml.etree.ElementTree as ET
doc = ET.fromstring(members)
for frame in doc.findall('.//frame'):         
       print(frame.attrib['x'])

输出:

414
333

编辑:

更改 xml 后,将 for 循环更改为:

for frame in doc.findall('.//frame[rectangle]'):    
      for rec in frame.findall('.//rectangle'):
         print(rec.attrib['x'])

现在输出应该是:

414
300
316
635