访问ElementTree子元素中的未命名标签

时间:2019-01-14 17:03:28

标签: python python-2.7 elementtree

我已经连续两天没有运气了,一直在阅读问题和示例,所以我认为现在是时候问我自己的问题了。我有一个正在处理的XML的子集。

我希望能够获得<value><text></value></text>子元素的值,但到目前为止我还无法实现。

    #This is an example of the data retrieved via the REST call in my code.
    <results>
        <result offset='0'>
            <field k='_bkt'>
                <value><text>_internal~12~5A0B5349-F3B5-46CD-83E0-3816A0969D51</text></value>
            </field>
            <field k='_cd'>
                <value><text>12:241565</text></value>
            </field>
            <field k='_eventtype_color'>
                <value><text>none</text></value>
            </field>
            <field k='_indextime'>
                <value><text>1547138402</text></value>
            </field>
            <field k='_kv'>
                <value><text>1</text></value>
            </field>
        </result>
    </results>

    result = urllib2.urlopen(request)
    data = result.read()

    tree = ET.fromstring(results)

    for leaf in tree:
        #just a txt file to dump output to
        info_f.write("Row: " + leaf.get('offset') + "\n")
        fields = leaf.findall('field')
        for field in fields:
            if 'k' in field.attrib:
                key = field.get('k')
                info_f.write("    Key: " + key + "\n")

上面的代码和示例数据正确地将偏移量输出为'0',并且正确地输出了命名标签,但是我尝试了很多方法来访问子元素的值,但是没有运气。

理想情况下,我的输出如下:

    Offset: 0
    key: _indextime
    value: 1547138402
    etc...

编辑:我已经在以下代码片段中使用getchildren()成功访问了元素,但在较新版本的python中已弃用了该元素。有没有更前瞻的思维,优雅的方法?

    result = urllib2.urlopen(request)
    data = result.read()

    tree = ET.fromstring(results)

    for leaf in tree:
        #just a txt file to dump output to
        info_f.write("Row: " + leaf.get('offset') + "\n")
        fields = leaf.findall('field')
        for field in fields:
            if 'k' in field.attrib:
                key = field.get('k')
                info_f.write("    Key: " + key + "\n")

                values = field.getchildren()                    
                for v in values:
                    temp = v.getchildren()                        
                    for t in temp:
                        info_f.write("    value: " + t.text + "\n")

1 个答案:

答案 0 :(得分:0)

您可以直接通过findtext()获取值。

for field in fields:
    if 'k' in field.attrib:
        key = field.get('k')
        value = field.findtext("value/text")
        info_f.write("    Key: " + key + "\n")
        info_f.write("    value: " + value + "\n")