說明:
請撰寫一程式,讀取新北市公共自行車即時資訊read.xml,請將其中sno(站點代號)、sna(中文場站名稱)、tot(場站總停車格)等三個欄位轉存為write.csv (需為UTF-8編碼格式),各欄位內容之間以一個半形逗號隔開。
提示:只需要輸出資料,不需要輸出欄位名稱。
檔案連結:read.xml (請另存檔案,必須與程式同一資料夾)
範例輸出:

程式碼:
import xml.etree.ElementTree as ET
tree = ET.ElementTree( file = 'read.xml' )
root = tree.getroot()
with open('write.csv', 'w', encoding='utf-8') as fp:
for row in root:
sno = row.find('sno').text
sna = row.find('sna').text
tot = row.find('tot').text
fp.write('{},{},{}\n'.format( sno, sna, tot ) )
沒有留言:
張貼留言