2016-08-31 16 views
0

のPython 2 は時にname属性によってPythonの変更XML属性

<Label name="qotl_type_label" position="910,980" font="headline_light" /> 

が検索のpythonを使用してXMLファイルを変更して位置を変更することはかのうですか?

+0

あなたの質問を明確にしてください。私が理解できるのは、xmlファイルを変更したいということです(どのように変更するのですか?) aが要件を満たしているか、ファイルを変更しているか、またはその両方を確認しているかどうかを確認する助けが必要ですか? – YakovL

+0

ファイルの属性を変更したい –

答えて

1

あなたはビルトインxml.etree.ElementTreeモジュール、XMLを解析Label要素を検索し、.attribプロパティを通じてposition属性を変更するために使用することができます:属性があり、属性の順序が保存されていないことを

>>> import xml.etree.ElementTree as ET 
>>> 
>>> s = '<root><Label name="qotl_type_label" position="910,980" font="headline_light" /></root>' 
>>> 
>>> root = ET.fromstring(s) 
>>> label = root.find(".//Label[@name='qotl_type_label']") 
>>> label.attrib['position'] = 'new,position' 
>>> ET.tostring(root) 
'<root><Label font="headline_light" name="qotl_type_label" position="new,position" /></root>' 

注意を順序不定によって定義されています。

+0

名前属性のみで検索する方法はありますか?これは私のプログラムでは面倒です。 –

+0

@KubaJanek私はコードサンプルでそれを正確に行いました。 'name'属性値をチェックする" find() "コールを見てください。 – alecxe

+0

申し訳ありませんが遅れています –

関連する問題