sagar846
sagar846

Reputation: 17

How do I extract specific data from xml using python?

I'm relatively new to python. I've been trying to learn python through a hands-on approach (I learnt c/c++ through the doing the euler project). Right now I'm learning how to extract data from files. I've gotten the hang of extracting data from simple text files but I'm kinda stuck on xml files. An example of what I was trying to do. I have my call logs backed up on google drive and they're a lot (about 4000) Here is the xml file example

<call number="+91234567890" duration="49" date="1483514046018" type="3" presentation="1" readable_date="04-Jan-2017 12:44:06 PM" contact_name="Dad" />

I want to take all the calls to my dad and display them like this

number = 234567890
duration = "49"  date="04-Jan-2017 12:44:06 PM"
duration = "x"   date="y"
duration = "n"   date="z"

and so on like that. How do you propose I do that?

Upvotes: 0

Views: 9041

Answers (1)

Anil_M
Anil_M

Reputation: 11453

It's advisable to provide sufficient information in a question so that problem can be recreated.

<?xml version="1.0" encoding="UTF-8"?>
<call number="+91234567890" duration="49" date="1483514046018" type="3" 
 presentation="1" readable_date="04-Jan-2017 12:44:06 PM" 
    contact_name="Dad" />

First we need to figure out what elements can we iter on. Since <call ../> is root element over here, we iter over that.

NOTE: if you have tags/element prior to the line provided, you will need to figure out proper root element instead of call.

>>> [i for i in root.iter('call')]
[<Element 'call' at 0x29d3410>]

Here you can see, we can iter on element call.

Then we simply iter over the element and separate out element attribute key and values as per requirements.

Working Code

import xml.etree.ElementTree as ET
data_file = 'test.xml'
tree = ET.parse(data_file)
root = tree.getroot()

for i in root.iter('call'):
    print 'duration', "=", i.attrib['duration']
    print 'data', "=", i.attrib['date']

Result

>>> 
duration = 49
data = 1483514046018
>>> 

Upvotes: 1

Related Questions