2016-04-12 8 views
2

私は現在、次のようになりバッチ編集多くのMusicXMLファイルへのPythonを使用しています:Python(elementtree)のmusicXMLツリーに新しい要素を追加するには?

<score-partwise> 
    ... 
     <attributes> 
     <transpose> 
      <diatonic>-5</diatonic> 
      <chromatic>-9</chromatic> 
      </transpose> 
     </attributes> 
    ... 
     </score-partwise> 

は、どのように私は以下のように、<transpose></transpose><octave-change>-1</octave-change>を追加することができますか?成功せず

import xml.etree.ElementTree as ET 

attributes = ET.Element("attributes") 
attributes.append(ET.fromstring('<transpose><octave-change>-1</octave-change></transpose>')) 

<score-partwise> 
    ... 
     <attributes> 
     <transpose> 
      <diatonic>-5</diatonic> 
      <chromatic>-9</chromatic> 
      <octave-change>-1</octave-change> 
      </transpose> 
     </attributes> 
    ... 
     </score-partwise> 

私はこれを試みてきました。

ご協力いただきありがとうございます。ありがとうございました。

+0

他の人がすることができますので、多分、小さな例期待されていたものを、あなたのコードを実行しているから生成されたものを出力見るために役立つこと、となり問題を再現してください。 – theWanderer4865

+0

@ theWanderer4865完全なコードが動作したら、完全なコードで質問を更新します。プログラムのポイントは、一度に何百ものmusicXMLファイルのメタデータの特定のフィールドを変更することです。したがって、ディレクトリ内の100個のファイルが「フルート」用である場合、ユーザはこのプログラムを使用して、所望のターゲット機器、例えば「oboe」に対する関連XMLメタデータを変更することができる。最終的にプログラムは新しいファイルを書き込みます。上記のコードは、完全なプログラムの一部です。 –

答えて

2

だけの要素を見つけて追加:

x = """<score-partwise>  
     <attributes> 
     <transpose> 
      <diatonic>-5</diatonic> 
      <chromatic>-9</chromatic> 
      </transpose> 
     </attributes>  
     </score-partwise>""" 

import xml.etree.ElementTree as et 
xml = et.fromstring(x) 

# 
xml.find("attributes").append(et.fromstring('<transpose><octave-change>-1</octave-change></transpose>')) 

print(et.tostring(xml)) 

あなたを与える:

<score-partwise> 
     <attributes> 
     <transpose> 
      <diatonic>-5</diatonic> 
      <chromatic>-9</chromatic> 
      </transpose> 
     <transpose><octave-change>-1</octave-change></transpose></attributes> 
</score-partwise> 

あなただけ選択し、既存の転置の要素に追加したい場合は、また新しい転置要素を追加することにそれ。

<score-partwise> 
     <attributes> 
     <transpose> 
      <diatonic>-5</diatonic> 
      <chromatic>-9</chromatic> 
      <octave-change>-1</octave-change></transpose> 
     </attributes> 
</score-partwise> 

また、あなたがノードにアクセスすることを可能にするSubElementを使用することができます:あなたを与える

import xml.etree.ElementTree as et 

xml = et.fromstring(x) 


xml.find(".//attributes/transpose").append(et.fromstring('<octave-change>-1</octave-change>')) 

print(et.tostring(xml)) 

あなたは書式設定したい場合は

xml = et.fromstring(x) 

print(et.tostring(xml)) 
e = et.SubElement(xml.find(".//attributes/transpose"), "octave-change") 
e.text = "-1" 
e.tail= "\n" 

、あなたはlxmlのを見つけるかもしれないより良い選択肢にする:

インポRT lxml.etreeら

書きます
parser = et.XMLParser(remove_blank_text=True) 
xml = et.parse("test.xml",parser) 


xml.find(".//attributes/transpose").append(et.fromstring('<octave-change>-1</octave-change>')) 
xml.write('test.xml', pretty_print=True) 

として:

<score-partwise> 
    <attributes> 
    <transpose> 
     <diatonic>-5</diatonic> 
     <chromatic>-9</chromatic> 
     <octave-change>-1</octave-change> 
    </transpose> 
    </attributes> 
</score-partwise> 
+0

'xml.find'を使用する代わりに' xml'はドキュメント自体からのテキストで定義した変数ですが、 'tree.find(" .// attributes/transpose ")を使うことができますか?append(et.fromstring( ' -1) ') '' tree = ET.parse(i) 'であり、' i'はforループでアクセスされるディレクトリの各ファイルですか? –

+0

自分自身を試してみましょう。私は報告します:) –

+0

'tree.find(" .// attributes/transpose ")append(et.fromstring( ' -1 ')) '私の最初のコメントで' tree'がどのように定義されているかによって実際に動作します。あなたの助けに感謝Padraic。 –

関連する問題