我有一个如下所示的 XML 文件。
<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="show_batch" value="true" />
<setting id="show_log" value="true" />
</settings>
我想在 XML 上运行 python 脚本,将信息提取到 cfg 文件中。 所以它看起来像这样。
auto_backup=false
exitonbackspace=true
nb_backupfiles=10
refreshonload=true
screen2=false
show_batch=true
show_log
我该如何解决这个问题?
最佳答案
#!/usr/bin/env python2.7
import sys, lxml.etree
tree = lxml.etree.parse(sys.stdin)
for el in tree.findall('setting'):
print el.attrib['id'], '=', el.attrib['value']
类似这样的事情。从标准输入转换为输出,因此您可以假设它是 script.py
:
cat settings.xml | ./script.py >> settings.cfg
关于python - XML 转换为 cfg 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16065292/