のPython 2 は時にname属性によってPythonの変更XML属性
<Label name="qotl_type_label" position="910,980" font="headline_light" />
が検索のpythonを使用してXMLファイルを変更して位置を変更することはかのうですか?
のPython 2 は時にname属性によってPythonの変更XML属性
<Label name="qotl_type_label" position="910,980" font="headline_light" />
が検索のpythonを使用してXMLファイルを変更して位置を変更することはかのうですか?
あなたはビルトイン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>'
注意を順序不定によって定義されています。
名前属性のみで検索する方法はありますか?これは私のプログラムでは面倒です。 –
@KubaJanek私はコードサンプルでそれを正確に行いました。 'name'属性値をチェックする" find() "コールを見てください。 – alecxe
申し訳ありませんが遅れています –
あなたの質問を明確にしてください。私が理解できるのは、xmlファイルを変更したいということです(どのように変更するのですか?) aが要件を満たしているか、ファイルを変更しているか、またはその両方を確認しているかどうかを確認する助けが必要ですか? – YakovL
ファイルの属性を変更したい –