2

I am new to Python.

I have a piece of python I have written to take out a data element I need from an XML that I have. The problem is I do not know how to repeat it to get out all the elements that I need.

import xml.etree.ElementTree as ET
import lxml.etree


doc = lxml.etree.parse('datafiles.xml')
total_datasets = doc.xpath('count//driversUsed)')


tree = ET.parse('datafiles.xml')
root = tree.getroot()

alias = root[1]
dataset = alias[0]
current = dataset[0]

print(current.text) 

So right now the current has the first value that I need but I need to do a loop where dataset = alias[1], dataset = alias[2] .... dataset = alias[total_datasets].

I've done a little bit of looping but I don't know how to do it where the variable is not just an integer but has the [] with the integer inside.

2
  • What do you mean take out a data element? Are you trying to create a new XML or extract data from existing XML? Commented Nov 19, 2015 at 2:45
  • I think import lxml.etree can do work whatever you want... Commented Nov 19, 2015 at 6:25

1 Answer 1

2

Use for and you don't need total_datasets

for dataset in alias:
    current = dataset[0]
    print(current.text) 

It is basic knowledge so better find some Python Tutorial.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.