我使用这段代码将XML文件转换为cfg文件
import sys, lxml.etree
doc = lxml.etree.parse('settings.xml')
foo = open('settings.cfg', 'w')
for el in doc.findall('setting'):
foo.write("%s=%s\n" % (el.attrib['id'], el.attrib['value']))
foo.close()
在我将标签写入文件之前,是否有一种方法可以对标签进行排序,并可能忽略某些标签。你知道吗
输入
<settings>
<setting id="auto_backup" value="false" />
<setting id="exitonbackspace" value="true" />
<setting id="hidemousepointer" value="true" />
<setting id="nb_backup_files" value="10" />
<setting id="refreshonload" value="true" />
<setting id="screen2" value="false" />
<setting id="separator" value="" />
<setting id="show_batch" value="true" />
<setting id="show_log" value="true" />
</settings>
电流输出
auto_backup=false
exitonbackspace=true
hidemousepointer=true
nb_backup_files=10
refreshonload=true
screen2=false
separator=
show_batch=true
show_log=true
需要输出
exitonbackspace=true
screen2=true
refreshonload=true
hidemousepointer=false
嗯…你的输入和当前输出似乎是按id排序的,你的“需要”输出似乎是随机的。那么,如何对您的输入进行排序,以获得所需的输出呢?我们需要安装mindreader模块吗?你知道吗
如果您的输入如下所示:
您可以按以下id排序:
为xml中找到的所有设置标记创建一个字典
settings_dict
。你知道吗创建一个列表
settings_order
,按照您希望设置出现的顺序。你知道吗遍历
settings_order
,如果settings_dict
中存在元素,则可以输出它。你知道吗相关问题 更多 >
编程相关推荐