Skip to main content
 首页 » 编程设计

python之lxml:获取属性值后的字段

2024年09月07日9落叶无声

我正在解析 XML 文件,我有一个来自 here 的后续问题.来自以下 XML 字段:

<enrollment type="Anticipated">30</enrollment> 

我想抽出预期这个词和数字。 在我拥有的文件中,“注册类型”/“注册”将在文件之间保持稳定,但“预期”不会(例如,有时它会说“实际”或其他内容)并且数字不会保持稳定。

我试过的代码:

from lxml import etree 
import sys 
import glob 
list_to_get = ['enrollment'] 
list_of_files = glob.glob('*xml') 
for each_file in list_of_files: 
#    try: 
        tree = etree.parse(each_file) 
        root = tree.getroot() 
        for node in root.xpath("//" + 'enrollment'): 
            for e in node.xpath('descendant-or-self::*[not(*)]'): 
                if e.attrib: 
                        print e.attrib 
                        print e.find('type') 
                        print e.find('.//type') 
                        print e.attrib['type'] 
                        print e.find(e.attrib['type']).text 

使用这种方法,我可以提取类型(例如预期/实际),但我找不到任何提取数字的方法。如果有人知道我应该使用的打印线,我将不胜感激。

我确实看过一些类似的问题(例如 here ),但他们的建议似乎对我不起作用。

请您参考如下方法:

您所做的一切都是正确的。只是不要复杂化。 简单来说,使用xpath获取根节点,使用getiterator迭代每个子节点,使用tag.text可以获取每个子节点的值

例子

parent 
    child 
    child 
 
for i in parent.getiterator(): 
    print(i.tag)#will give the first child tag 
    print(i.text)#Will give the value