Created
January 6, 2010 22:04
-
-
Save messa/270719 to your computer and use it in GitHub Desktop.
Python XML example (etree)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<doc> | |
<person no="1234"> | |
<name>Tomáš</name> | |
<surname>Těžký</surname> | |
</person> | |
<person no="7"> | |
<name>Standa</name> | |
<surname>Blábol</surname> | |
</person> | |
</doc> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from xml.etree.ElementTree import ElementTree | |
tree = ElementTree() | |
tree.parse("data.xml") | |
for person in tree.getiterator("person"): | |
print person.attrib["no"] | |
print person.find("name").text, person.find("surname").text | |
print "--" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./example.py | |
1234 | |
Tomáš Těžký | |
-- | |
7 | |
Standa Blábol | |
-- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment