简体   繁体   中英

ElementTree unique Tags

There's a problem with the below code, since the clist array will contain every tag, but I need it to remove any possible duplicates. I can't find a way to get each e.tag results individually, which results in it parsing the entire taglist into the array, one taglist at a time, so it ends up giving you an array consisting of arrays.

Problematic code:

import xml.etree.ElementTree as ET
tree = ET.parse('example.xml').getroot()
for nametag in tree.iter('nametag'):
    elems = [e for e in nametag.iter() if len(e.text) > 1]
    clist = []
    for e in elems:
        if not e.tag in clist:
            clist.append(e.tag)

问题尚不清楚,但是如果您要查找文档中使用的所有标签名称,则应执行以下操作。

tag_names = set([e.tag for e in tree.xpath('//*')])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM