我有三個XML文件(下面的示例)。我已經使用它們各自的audioId
屬性值命名了這些文件。因此,有問題的文件將被稱爲93.xml
和2137.xml
:解析來自一個文件的特定XML屬性並將其附加到另一個iff另一個屬性出現在第二個文件中
93.xml:
<word BloomsTaxonomy="1,2,3" DictationGroupid="i-e combination List 7" Stage="0" Use="P,L" audioId="93" />
2173.xml:
<word BloomsTaxonomy="1,2,3" DictationGroupid="i-e combination List 7" Stage="0" Use="P,L" audioId="2137" />
mainDataSet.xml:
<word id="2137" title="over" level="1" grouping="Sight Words" YRule="0" MagicE="0" SoftC="0" doublevowel="0" longvowel="0" displayorder="101" silentletters="0"/>
文件mainDataSet.xml
包含〜3,000條目。爲了這個問題,我只提供了一個條目。
我的問題是關於如何我就從mainDataSet.xml
從mainDataSet.xml
追加title
屬性爲word
標籤中2173.xml
如果在這兩個文件的id
比賽(或者即使在mainDataSet.xml
的id
相匹配的文件名)。例如,我所提供的樣品中,輸出應該是:
<word BloomsTaxonomy="1,2,3" DictationGroupid="i-e combination List 7" Stage="0" Use="P,L" audioId="2137" title="over" />
從mainDataSet.xml
解析我的XML,我正在做的:
e = xml.etree.ElementTree.parse('mainDataSet.xml').getroot()
for atype in e.findall('word'):
print(atype.get('title'))
完美。丹科!我會通過這個,讓你知道它是怎麼回事! – thel3l