Question

For some reasons I just can't extract the content of the xml below: Can somebody please point me in the right direction?

f='''<features>
    <name>Michael</name>
    <age>81</age>
    <dob>20/10/1925</dob>
</features>'''

Attempt:

tree=ET.parse(StringIO(f))
root=tree.getroot()
for x in root:
    print x.tag, x.attrib

Output:
name {}
age {}
dob {}

Ideal Output:

name {Michael}
age {81}
dob {20/10/1925}

Not really good at xml with python, thanks for your suggestions?

Était-ce utile?

La solution

Your elements do not have attribs. Try x.text instead, although you will need to add curly braces if you really want the text in curly braces.

>>> import xml.etree.cElementTree as et
>>> root = et.fromstring(f)
>>> for x in root:
    print x.tag, x.text


name Michael
age 81
dob 20/10/1925
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top