如何解决使用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_转换为_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' 对象没有属性 'attrib'
2.
member.find('rectangle').attrib['x']
AttributeError: 'list' 对象没有属性 '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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。