1
我是一個新的Python所以請原諒,我試圖解釋什麼,我試圖做蟒蛇提取XML元素值到csv
,這裏是我的xml
<?xml version="1.0"?>
<playlist>
<list>
<txdate>2015-10-30</txdate>
<channel>cake</channel>
<name>Play List</name>
</list>
<eventlist>
<event type="MEDIA">
<title>title1</title>
<starttype>FIX</starttype>
<mediaid>a</mediaid>
<onairtime>2015-10-30T13:30:00:00</onairtime>
<som>00:00:40:03</som>
<duration>01:15:47:15</duration>
<reconcilekey>123</reconcilekey>
<category>PROGRAM</category>
<subtitles>
<cap>CLOSED</cap>
<file>a</file>
<lang>ENG</lang>
<lang>GER</lang>
</subtitles>
</event>
<event type="MEDIA">
<title>THREE DAYS AND A CHILD</title>
<mediaid>b</mediaid>
<onairtime>2015-10-30T14:45:47:15</onairtime>
<som>00:00:00:00</som>
<duration>01:19:41:07</duration>
<reconcilekey>321</reconcilekey>
<category>PROGRAM</category>
<subtitles>
<cap>CLOSED</cap>
<file>b</file>
<lang>ENG</lang>
<lang>GER</lang>
</subtitles>
</event>
</eventlist>
</playlist>
我會想打印的所有mediaid值到文件 這是我到目前爲止的代碼
import os
import xml.etree.ElementTree as ET
tree = ET.parse('data.xml')
root = tree.getroot()
wfile = 'new.csv'
for child in root:
child.find("media type")
for x in child.iter("mediaid"):
file = open(wfile, 'a')
file.write(str(x))
file.close
我試圖與其他一些非標準的庫,但我並沒有太多的成功
那麼你期待在文件中輸出什麼?對於上面的例子xml? –
「a」在第一行「b」在第二行 – lemons
所以你只需要文件中的所有'mediad'值?沒有任何條件,對嗎? –