Python and xml
Konstantin Veretennicov
kveretennicov at gmail.com
Sat Mar 29 18:12:52 EDT 2008
On Sat, Mar 29, 2008 at 3:57 PM, Doran, Harold <HDoran at air.org> wrote:
> I am a python neophyte who has used python to parse through text files
> using basic functions and no OOP experience. I have a need to process some
> xml files and from what I am now reading python is the exact tool I need to
> work through this issue.
>
> However, as I read online docs and peruse which books to buy, I am quickly
> becoming overwhelmed with the topic and could use some guidance on how to
> best tackle my task.
>
> You can start with this basic example (requires Python 2.5):
spam.xml:
<monty>
<episode number="14">Dinsdale (Face the Press)</episode>
<episode number="15">The Spanish Inquisition</episode>
<episode number="16">Deja vu</episode>
<episode number="17">The Buzz Aldrin Show</episode>
<episode number="18">Live From the Grill-O-Mat Snack Bar</episode>
<episode number="19">It's a Living</episode>
<episode number="20">The Attila the Hun Show</episode>
<episode number="21">Archaeology Today</episode>
<episode number="22">How to Recognize Different Parts of the
Body</episode>
<episode number="23">Scott of the Antarctic</episode>
<episode number="24">How Not to Be Seen</episode>
<episode number="25">Spam</episode>
<episode number="26">Royal Episode 13</episode>
</monty>
spam.py:
from xml.etree.ElementTree import ElementTree as ET
et = ET(file='spam.xml')
for episode in et.findall('episode'):
print episode.attrib['number'] + ':', '"' + episode.text + '"'
Use standard csv module if you want to produce csv ouput (
http://docs.python.org/lib/module-csv.html).
--
kv
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080330/d1142f55/attachment-0001.html>
More information about the Python-list
mailing list