缺少在python节点中创建XML文件

缺少在python节点中创建XML文件,python,xml,pandas,elementtree,Python,Xml,Pandas,Elementtree,我想用elementtree库创建一个XML文件 XML文件应如下所示: <files> <file> <ans>EP16</ans> <ep></ep> <date>2017-03-15</date> <concepts>~what</concepts> </file> <

我想用elementtree库创建一个XML文件

XML文件应如下所示:

<files>
    <file>
        <ans>EP16</ans>
        <ep></ep>
        <date>2017-03-15</date>
        <concepts>~what</concepts>
    </file>
    <file>
        <ans>EP17</ans>
        <ep>ep6665</ep>
        <date>2017-03-15</date>
        <concepts>~whatever</concepts>
     </file>
     etc
</files>
保存文件:

tree2 = ET.ElementTree(XMLfiles)
filetosave=os.path.join('00DATA_output','bb.xml')
tree2.write(filetosave)
创建了一个XML文件,但它跳过了每个文件的关闭。创建的xml文件开始时为:

<files>
    <file>
        <ans>EP16</ans>
        <ep></ep>
        <date>2017-03-15</date>
        <concepts>~what</concepts>
   ... ***** closing and open <file> is missing
        <ans>EP17</ans>
        <ep>ep6665</ep>
        <date>2017-03-15</date>
        <concepts>~whatever</concepts>
    </file>
</files>

EP16
2017-03-15
~z~什么
... ***** 缺少“关闭”和“打开”
EP17
ep6665
2017-03-15
~z~随便了
每次打开和关闭文件的代码中缺少什么? 注意:假设正在解析的df是正常的,序列是df的一行。

如果您希望在
dffiles
中的每一行都有一个
标记,那么也将其移动到循环中

nrofrows = dffiles.shape[0]
for i in range(nrofrows):
    file = ET.SubElement(XMLfiles, "file")
    serie = dffiles.iloc[i]
    child1 = ET.SubElement(file, "an")
    child1.text = serie[0]
    child2 = ET.SubElement(file, "ep")
    child2.text = serie[1]
    child3 = ET.SubElement(file, "date")
    child3.text = serie[2]
    child4 = ET.SubElement(file, "concepts")
    child4.text = serie[3]
nrofrows = dffiles.shape[0]
for i in range(nrofrows):
    file = ET.SubElement(XMLfiles, "file")
    serie = dffiles.iloc[i]
    child1 = ET.SubElement(file, "an")
    child1.text = serie[0]
    child2 = ET.SubElement(file, "ep")
    child2.text = serie[1]
    child3 = ET.SubElement(file, "date")
    child3.text = serie[2]
    child4 = ET.SubElement(file, "concepts")
    child4.text = serie[3]