1

このような 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>

情報を cfg ファイルに抽出する XML で python スクリプトを実行したいと思います。したがって、次のようになります。

auto_backup=false
exitonbackspace=true
nb_backupfiles=10
refreshonload=true
screen2=false
show_batch=true
show_log

これについてどうすればいいですか?

4

1 に答える 1

1
#!/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
于 2013-04-17T16:54:02.787 に答える