如何遍历etree.findall()中的列表

时间:2013-08-16 15:05:09

标签: python lxml python-3.3

我想做下面的工作但是很难过。如何在for循环中循环ns

ns = ['one', 'two']

tree = etree.parse(file) 
for items in tree.findall('.//' + ns):
    print(items)

编辑: 寻找没有嵌套for循环的解决方案。

编辑:

   ns = ['{http://purl.org/dc/elements/1.1/}identifier[@{http://www.w3.org/2001/XMLSchema-instance}type]',
           '{http://purl.org/dc/elements/1.1/}title',
           '{http://purl.org/dc/elements/1.1/}description',
           '{http://purl.org/dc/elements/1.1/}subject',
           '{http://purl.org/dc/elements/1.1/}type',
           '{http://purl.org/dc/terms/}educationLevel']
   tree = etree.parse(file)
   for leaf in tree.xpath('//*[local-name()="record"]'):
       for items in leaf.findall('.//' + ns[0]):
           print(items)

1 个答案:

答案 0 :(得分:1)

使用xpath

from lxml import etree

nsmap = {
    'xmlns': 'http://www.openarchives.org/OAI/2.0/',
    'dc': 'http://purl.org/dc/elements/1.1/',
    'dct': 'http://purl.org/dc/terms/',
    'xsi': 'http://www.w3.org/2001/XMLSchema-instance',
}
ns = [
    'dc:identifier[@xsi:type]',
    'dc:title',
    'dc:description',
    'dc:subject',
    'dc:type',
    'dct:educationLevel'
]
tree = etree.parse(file)
xpath = '|'.join('.//xmlns:record//{}'.format(n) for n in ns)
for items in tree.xpath(xpath, namespaces=nsmap):
    print(items)