1

I have XML stored as a string "vincontents", formatted as such:

<response>
    <data>
        <vin>1FT7X2B69CEC76666</vin>
    </data>
    <data>
        <vin>1GNDT13S452225555</vin>
    </data>
</response>

I'm trying to use Python's elementtree library to parse out the VIN values into an array or Python list. I'm only interested in the values, not the tags.

def parseVins():
content = etree.fromstring(vincontents)
vins = content.findall("data/vin")
print vins

Outputs all of the tag information:

[<Element 'vin' at 0x2d2eef0>, <Element 'vin' at 0x2d2efd0> ....

Any help would be appreciated. Thank you!

0

1 Answer 1

4

Use .text property:

>>> 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']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.