Parse XML with Python

by Süleyman Petek 10. Haziran 2016 12:12
Here i am again with a small blog post, a life saver small Python script to parse your XML files.I s

Here i am again with a small blog post, a life saver small Python script to parse your XML files.

I suppose you have an XML file like below that you exported from some other tool ;

<schedule>
    <job name="www.mysite.com">
    <job name="www.mysite.com">
    ...
</schedule>

And you want to get all the "name" attribute values from the XML structure, then you can write Java or C Sharp or any other language to parse this file however i think Python is the easiest and the most speed one.

It is as short as below:

import xml.etree.ElementTree
f=open("jobs.txt","w")
e=xml.etree.ElementTree.parse('schedule.xml').getroot()
for atype in e.findall('job'):
f.write(atype.get('name')+"\n")
f.close()

I just create a .txt file here called "jobs.txt" and write all the job names to it.

Enjoy...


Tags: ,

Life Saver | Tip

Calendar

<<  Temmuz 2018  >>
PztSalÇarPerCumCmtPaz
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

View posts in large calendar

RecentPosts