我将XML存储为字符串" vincontents",格式如下:
<response>
<data>
<vin>1FT7X2B69CEC76666</vin>
</data>
<data>
<vin>1GNDT13S452225555</vin>
</data>
</response>
我试图使用Python的elementtree库将VIN值解析为数组或Python列表。我只对价值感兴趣,而不是标签。
def parseVins():
content = etree.fromstring(vincontents)
vins = content.findall("data/vin")
print vins
输出所有标签信息:
[<Element 'vin' at 0x2d2eef0>, <Element 'vin' at 0x2d2efd0> ....
任何帮助将不胜感激。谢谢!
答案 0 :(得分:3)
使用.text
属性:
>>> import xml.etree.ElementTree as etree
>>> data = """<response>
... <data>
... <vin>1FT7X2B69CEC76666</vin>
... </data>
... <data>
... <vin>1GNDT13S452225555</vin>
... </data>
... </response>"""
>>> tree = etree.fromstring(data)
>>> [el.text for el in tree.findall('.//data/vin')]
['1FT7X2B69CEC76666', '1GNDT13S452225555']