2016-02-10 114 views

回答

1

首先,你需要解析你的XML文件。這可以通過ElementTree API來完成:

示例代碼:

import xml.etree.ElementTree as ET 
root = ET.parse('your_data.xml').getroot() 
with open("output.csv", "w") as file: 
    for child in root: 
     print(child.tag, child.attrib) 
     # naive example how you could save to csv line wise 
     file.write(child.tag+";"+child.attrib) 

There are also solutions to parse your XMLs directly as dictionary.

然後csv.DictWriter可用於保存字典作爲CSV。