ElementTree元素索引查找 [英] ElementTree element index look up

查看:73
本文介绍了ElementTree元素索引查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 xml.etree.ElementTree 模块从另一个结构化文档中使用Python 3.1创建一个XML文档。

I'm using the xml.etree.ElementTree module to create an XML document with Python 3.1 from another structured document.

我可以使用哪个ElementTree函数返回现有子元素的索引?

What ElementTree function can I use that returns the index of an existing subelement?

推荐答案

getchildren方法返回一个元素对象的子元素列表。然后,您可以使用列表的内置索引方法。

The getchildren method returns a list of sub-elements of an Element object. You could then use the built-in index method of a list.

>>> import xml.etree.ElementTree as ET
>>> root = ET.Element("html")
>>> head = ET.SubElement(root, "head")
>>> body = ET.SubElement(root, "body")
>>> root.getchildren().index(body)
1

这篇关于ElementTree元素索引查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆