Python - 将 XML 转换为 CSV
大家下午好.
我将在这个问题的开头说这是我第一次涉足 Python.我正在使用 API 返回以下 XML 示例:
I will preface this question by saying that this is my first foray into Python. I am using an API to return the following XML sample:
<Times>
<Time>
<ID> 120877787 </ID>
<Job>
<ID> J000050 </ID>
<Name> My Job </Name>
</Job>
<Task>
<ID> 59469972 </ID>
<Name> My Task </Name>
</Task>
<Staff>
<ID> 74268 </ID>
<Name> My Name </Name>
</Staff>
<Date> 2017-05-19T00:00:00 </Date>
<Minutes> 480 </Minutes>
<Note/>
<Billable> true </Billable>
</Time>
</Times>
我目前正在使用 Python 3.4 将 XML 转换为 CSV.
I am presently in the process of converting the XML to CSV using Python 3.4.
我做了一些研究(http://blog.appliedinformaticsinc.com/how-to-parse-and-convert-xml-to-csv-using-python/ 例如)解决问题,但我可以没有想出合适的结果,主要是因为我对语法的理解不够好,无法根据我的具体情况进行调整.
I have done a fair bit of research (http://blog.appliedinformaticsinc.com/how-to-parse-and-convert-xml-to-csv-using-python/ for example) into resolving the issue, but I can't come up with a suitable result, primarily because I don't understand the syntax well enough to adapt it to my exact circumstance.
基本上我正在寻找以下输出.
Basically I am looking for the following output.
Job Name Task Name Staff Name Date Minutes Billable
My Job My Task My Name 2017-05-19T00:00:00 480 true
根据要求,这是从 API 返回 XML 的方式(作为字符串并通过 print(ts.content) 查看),这可能是我出错的地方.
As requested this is how the XML is returned from the API (as a string and viewed by print(ts.content)), which may be where I am going wrong.
<Times><Time><ID> 120877787 </ID><Job><ID> J000050 </ID><Name> My Job </Name></Job><Task><ID> 59469972 </ID><Name> My Task </Name></Task><Staff><ID>74268</ID><Name> My Name </Name></Staff><Date> 2017-05-19T00:00:00 </Date><Minutes> 480 </Minutes><Note/><Billable> true </Billable></Time></Times>
有人可以提供一些有关处理此任务的最佳方法的见解吗?
Could someone please offer some insight into the best way to approach this task?
感谢您的帮助.
斯科特
您可以使用 findall
功能.
import xml.etree.ElementTree as ET
import csv
tree = ET.parse("/temp/test.xml")
root = tree.getroot()
f = open('/temp/test.csv', 'w')
csvwriter = csv.writer(f)
count = 0
head = ['Job Name','Task Name','Staff Name','Date','Minutes','Billable']
csvwriter.writerow(head)
for time in root.findall('Time'):
row = []
job_name = time.find('Job').find('Name').text
row.append(job_name)
task_name = time.find('Task').find('Name').text
row.append(task_name)
staff_name = time.find('Staff').find('Name').text
row.append(staff_name)
date = time.find('Date').text
row.append(date)
minutes = time.find('Minutes').text
row.append(minutes)
billable = time.find('Billable').text
row.append(billable)
csvwriter.writerow(row)
f.close()
给出:
Job Name,Task Name,Staff Name,Date, Minutes,Billable
My Job , My Task , My Name , 2017-05-19T00:00:00 , 480 , true