首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ElementTree元素索引查找

ElementTree元素索引查找
EN

Stack Overflow用户
提问于 2010-09-22 01:54:45
回答 3查看 14.1K关注 0票数 13

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

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

EN

回答 3

Stack Overflow用户

发布于 2010-09-22 02:00:58

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

代码语言:javascript
复制
>>> 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
票数 12
EN

Stack Overflow用户

发布于 2017-01-04 15:30:58

代码语言:javascript
复制
import xml.etree.ElementTree as ET
root=ET.Element('C:\Users\Administrator\Desktop\ValidationToolKit_15.9\ValidationToolKit_15.9\NE3S_VTK\webservice\history\ofas.2017-1-3.10-55-21-608.xml')
childnew=ET.SubElement(root,"354")
root.getchildren().index(childnew)
0
list(root).index(childnew)
0
票数 0
EN

Stack Overflow用户

发布于 2017-01-05 16:46:25

代码语言:javascript
复制
def Alarms_Validation(self,path,AlarmNo,AlarmText,Severity,Text,Event):
    with open(path) as f:
        tree = et.parse(f)
        RUN=True
        root = tree.getroot()
        try:

                for x in xrange(10000):
                        print x
                        for y in xrange(6):
                                print y
                                if root[x][y].text==AlarmNo:
                                        print "found"
                                        print x
                                        print y
                                        if root[x][y+1].text!=AlarmText:
                                            print "Alarm text is not proper"
                                        else:
                                            print "Alarm Text is proper"
        except IndexError:
                pass
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3763048

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档