我想做以下工作,但很难。我如何ns
在 for 循环中循环?
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)