私はこのフォーラムの初心者です。
xml.etree.cElementTree からデータを取得しようとしています。
私は次のコードを持っています
コードスニペット
import xml.etree.cElementTree as ET
xmldata ="""
<pipeline>
<ep_150>
<stage name="lay" longname="layout" department="layout" process="production">
<review name="R1" reviewer="sridhar reddy" role="supervisor" id="p1234">
</review>
</stage>
<stage name="lip" longname="lipsync" department="lipsync" process="production">
<review name="R2" reviewer="someone" role="supervisor" id="p2345">
</review>
</stage>
<stage name="blk" longname="blocking" department="animation" process="production">
<review name="R3" reviewer="sudeepth" role="supervisor" id="p4645" dependson='R1'>
</review>
<review name="R4" reviewer="chandu" role="director" id="p5678">
</review>
</stage>
<stage name="pri" longname="primary" department="animation" process="production">
<review name="R5" reviewer="sudeepth" role="supervisor" id="p4645" style="dep" >
</review>
<review name="R6" reviewer="sudeepth" role="bld_supervisor" id="p2556" style="dep">
</review>
</stage>
<stage name="sec" longname="secondary" department="animation" process="production">
<review name="R7" reviewer="sha" role="supervisor" id="p1234" style="dep">
</review>
<review name="R8" reviewer="chandu" role="director" id="p5678">
</review>
</stage>
</ep_150>
</pipeline>
"""
root = ET.fromstring(xmldata)
stages = root.findall("./ep_150/stage")
print 'Stages in animation department....\n'
for stage in stages:
if stage.attrib['department']=='animation':
print stage.attrib['name']
review = root.findall("./ep_150/stage/review")
print '\n\nreviews for id=p4645\n'
for rev in review:
if rev.attrib['id']=='p4645':
print (rev.attrib['name'])
上記のコードを使用すると、以下のような結果が得られます
アニメーション部門のステージ....
黒い
プリ
秒
id=p4645 のレビュー
R3
R5
しかし、後半の出力が必要です
id=p4645 のレビュー
黒 - R3
プリ - R5
つまり、要素の親タグが必要です