简体   繁体   English

Python:lxml findall返回对象,而不是元素文本内的值

[英]Python: lxml findall returning object, not value inside element text

I have written the following python code to parse a XML file with lxml. 我编写了以下python代码以使用lxml解析XML文件。 I am confused why it is returning a memory address but not the actual output height in this case. 我很困惑为什么在这种情况下它返回的是内存地址而不是实际的输出height

import pdb
from pprint import pprint
from lxml import etree

def main():
    parser = etree.XMLParser(strip_cdata = False)
    tree = etree.parse("xml.xml", parser)
    string =  etree.tostring(tree.getroot())
    content = etree.fromstring(string)
    bodies = content.findall('Body')
    all_data = []
    for body in bodies:
        row  = {}
        height = body.findall('height')

        row['height'] = height
        all_data.append(row)
    print all_data
    pdb.set_trace()    

The output that I got is: 我得到的输出是:

[{'height': [<Element height at 0x11ac80830>]}]

whereas I expect a height of 178 . 而我预计身高为178

The data within the xml file is: xml文件中的数据是:

<Bodies>
  <Body>
    <name><![CDATA[abc]]></name>
       <body_name><![CDATA[asdjakhdas da sdasda sd]]></body_name>
       <final_height><![CDATA[199]]></final_height>
       <categories><![CDATA[a / b / c]]></categories>
       <hand><![CDATA[asdkj]]></hand>
        <height>178</height>
  </Body>
<Bodies>

If you don't want to put the height element into your variable, but instead the text field it contains: 如果您不想将height 元素放入变量中,而是将其包含在文本字段中:

height = body.find('height')
row['height'] = float(height.text) if height else None

Note the use of find as opposed to findall -- findall returns a list, whereas it sounds like you're expecting only a single value (and have no need to recurse). 请注意,使用find而不是使用findallfindall返回一个列表,而听起来您只希望使用一个值(并且不需要递归)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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