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

How to call batch files via Python

by Süleyman Petek 10. Mart 2016 17:26
Gentle reminder for me and anyone who needs in the future, say that you have lots of .bat files and

Gentle reminder for me and anyone who needs in the future, say that you have lots of .bat files and you need to call them all.

You can do it via Python like below :


import glob  

import os 

os.chdir('c:\YourBatFiles')  

for filename in glob.iglob('*.bat', recursive=True): 

print(filename)

os.system(filename)


Tags:

Tech | Tip

Calendar

<<  Kasım 2018  >>
PztSalÇarPerCumCmtPaz
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

View posts in large calendar

RecentPosts