python numpy数组转换为xml

python numpy数组转换为xml,python,xml,numpy,xml.etree,Python,Xml,Numpy,Xml.etree,例如,有我的数据: import numpy as np a = np.array([[ 26.2, 280. ], [ 26.2, 279. ], [ 26. , 278.8], [ 25.2, 278. ], [ 25. , 277.8], [ 24.2, 277. ], [ 24. , 276.8], [ 23. , 276.8], [ 22.8, 277. ],

例如,有我的数据:

import numpy as np
a = np.array([[ 26.2, 280. ],
       [ 26.2, 279. ],
       [ 26. , 278.8],
       [ 25.2, 278. ],
       [ 25. , 277.8],
       [ 24.2, 277. ],
       [ 24. , 276.8],
       [ 23. , 276.8],
       [ 22.8, 277. ],
       [ 23. , 277.2],
       [ 23.8, 278. ],
       [ 24. , 278.2],
       [ 24.8, 279. ],
       [ 25. , 279.2],
       [ 25.8, 280. ],
       [ 26. , 280.2],
       [ 26.2, 280. ]])
我想得到一个如下的xml文件:

<?xml version="1.0"?>
<ASAP_Annotations>
    <Annotations>
        <Annotation Name="Annotation 0" Type="Spline" PartOfGroup="_1" Color="#F4FA58">
            <Coordinates>
                <Coordinate Order="0" X="26.2" Y="280." />
                <Coordinate Order="1" X="26.2" Y="279. " />
                <Coordinate Order="2" X="26." Y="278.8" />
                .........
                .........
                .........
                <Coordinate Order="14" X="25.8" Y="280." />
                <Coordinate Order="15" X="26." Y="280.2" />
                <Coordinate Order="16" X="26.2" Y="280." />
            </Coordinates>
        </Annotation>
    </Annotations>
</ASAP_Annotations>

.........
.........
.........

我知道它可能是通过
xml.etree.ElementTree
实现的,但我从来没有学过它,你能告诉我如何在python中实现它吗。谢谢~

一个简单的for循环可以做到这一点:

headerXML = '''<?xml version="1.0"?>
<ASAP_Annotations>
    <Annotations>
        <Annotation Name="Annotation 0" Type="Spline" PartOfGroup="_1" Color="#F4FA58">
            <Coordinates>
'''

elements = ""

i = 0
for element in a:
    elements = elements+ '<Coordinate Order="%s" X="%s" Y="%s" />\n'% (i,element[0],element[1] )
    i = i + 1


footerXML = '''
            </Coordinates>
        </Annotation>
    </Annotations>
</ASAP_Annotations>
'''

XML =(headerXML+elements+footerXML)

outFile = open("coordinates.xml","w")
outFile.write(XML)
outFile.close()
headerXML=''
'''
elements=“”
i=0
对于a中的元素:
elements=elements+“\n%”(i,元素[0],元素[1])
i=i+1
footerXML=''
'''
XML=(headerXML+elements+footerXML)
outFile=open(“coordinates.xml”、“w”)
outFile.write(XML)
outFile.close()

您能告诉我如何将其写入xml文件吗?我编辑了答案再次检查,删除了第一行,这是一个空行