通过遍历列表使用

通过遍历列表使用

本文介绍了通过遍历列表使用 python 创建 XML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取一个看起来像这样的 xml 文件,以便我可以将其导入到 Moodle 成绩簿中:

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>

我尝试用 python 编写一些代码,在仔细阅读文档后,我能想到的最好的方法是:

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>

我试图找到一个解决方案,但似乎大多数问题都解决了编写具有不同元素的 xml 文件的问题.我想不通的是如何使用相同的元素标签但为每个标签分配不同的值.

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.

推荐答案

你需要做两件事:

  • Eli 建议的行移到 for 循环中.
  • 从 for 循环中删除行:results.append(result).SubElement 工厂将父元素作为它的第一个参数(如您所知)并将新创建的实例附加父母.因此你不需要再次追加它(你不会得到任何错误,但你会得到一组与那些不同的结果您指定了).
  • 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 thatparent. Therefore you don't need to append it again (you won't getany error, but you will get a different set of results than the onesyou 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])

这篇关于通过遍历列表使用 python 创建 XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:01