简体   繁体   中英

Create XML file with python by iterating over lists

I am trying to get an xml file that looks like this so that I can import it into a moodle gradebook:

<results>
  <result>
    <student>1</student>
    <assignment>100</assignment>
    <score>0</score>
  </result>
  <result>
    <student>1</student>
    <assignment>101</assignment>
    <score>4</score>
  </result>
  <result>
    <student>1</student>
    <assignment>102</assignment>
    <score>10</score>
  </result>
  <result>
    <student>2</student>
    <assignment>100</assignment>
    <score>0</score>
  </result>
  <result>
    <student>2</student>
    <assignment>101</assignment>
    <score>4</score>
  </result>
  <result>
    <student>2</student>
    <assignment>102</assignment>
    <score>10</score>
  </result>
</results>

I have tried to write some code in python, and after poring over the documentation, the best I can come up with is:

import xml.etree.cElementTree as ET

students = [1,2]
assignments=[100,101,102]
scores=[0,4,10]
results = ET.Element("results")
result = ET.SubElement(results,"result")
student = ET.SubElement(result,"student")
assignment = ET.SubElement(result,"assignment")
score = ET.SubElement(result,"score")

for s in students:    
    for a in range(len(assignments)):
        student.text = str(s)
        assignment.text = str(assignments[a])
        score.text = str(scores[a])
        results.append(result)
tree = ET.ElementTree(results)
tree.write('test.xml')

This almost gives me what I want, but the last entry seems to overwrite all the previous entries as shown:

<results>
  <result>
    <student>2</student>
    <assignment>102</assignment>
    <score>10</score>
  </result>
  <result>
    <student>2</student>
    <assignment>102</assignment>
    <score>10</score>
  </result>
  <result>
    <student>2</student>
    <assignment>102</assignment>
    <score>10</score>
  </result>
  <result>
    <student>2</student>
    <assignment>102</assignment>
    <score>10</score>
  </result>
  <result>
    <student>2</student>
    <assignment>102</assignment>
    <score>10</score>
  </result>
  <result>
    <student>2</student>
    <assignment>102</assignment>
    <score>10</score>
  </result>
  <result>
    <student>2</student>
    <assignment>102</assignment>
    <score>10</score>
  </result>
</results>

I have tried to find a solution, but it seems that most of the questions address just writing an xml file with distinct elements. What I can't figure out is how to use the same element tag but to assign distinct values to each one.

You need to move these lines

result = ET.SubElement(results,"result")
student = ET.SubElement(result,"student")
assignment = ET.SubElement(result,"assignment")
score = ET.SubElement(result,"score")

To inside the for loop. Otherwise you're just adding the same result over and over again.

You need to do two things:

  • Move the lines Eli suggested into the for loop.
  • Remove the line: results.append(result) from the for loop. The SubElement factory takes as its first argument a parent element (as you know) and it appends the newly created instance to that parent. Therefore you don't need to append it again (you won't get any error, but you will get a different set of results than the ones you specified).

You loop should then look like:

for s in students:    
    for a in range(len(assignments)):

        result = ET.SubElement(results,"result")
        student = ET.SubElement(result,"student")
        assignment = ET.SubElement(result,"assignment")
        score = ET.SubElement(result,"score")

        student.text = str(s)
        assignment.text = str(assignments[a])
        score.text = str(scores[a])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM