使用python创建数据.xml融合图文件

2024-04-27 00:24:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python的新手。我只想知道如何创建代码来编写xml文件名数据.xml对于熔炉。xml文件的格式如下:

<?xml version="1.0" encoding="UTF-8" ?>
<graph caption="Test graph" xAxisName="X" yAxisName="Y" showAnchors="1" anchorRadius="1" showValues="0">
<set name='2004' value='37800' color='AFD8F8' />
<set name='2005' value='21900' color='F6BD0F' />
<set name='2006' value='32900' color='8BBA00' />
<set name='2007' value='39800' color='FF8E46' />
</graph>

谢谢。在


Tags: 文件数据代码namevalue文件名version格式
3条回答

您可以使用python 2.0及更高版本中内置的miniDOM库:

http://docs.python.org/library/xml.dom.minidom.html

Dive Into Python 3中有一个很好的chapter on XML,如果您正在使用的是python2.x,那么其中大多数也适用于python2.x。在开始使用Python时,书籍/网站也是许多其他主题的优秀资源。在

stdlib中的ElementTree可能有帮助:

import xml.etree.cElementTree as etree

G = etree.Element('graph', dict(caption='Test graph', xAxisName="..."))

for name, value, color in get_sets():
    etree.SubElement(G, 'set', dict(name=name, value=value, color=color))

etree.ElementTree(G).write(open('data.xml', 'wb'), 
                           encoding='utf-8', xml_declaration=True))

相关问题 更多 >