import csv
import xml.etree.ElementTree
e = xml.etree.ElementTree.parse('test.xml').getroot()
with open('out.csv', 'w', newline='') as csv_file:
csv_writer = csv.writer(csv_file)
for data in e.iter('measures'):
measures = data.findall('measure/measure')
for measure in measures:
for row in measure:
csv_writer.writerow([row.get('timestamp'), row.get('max'), row.get('count'), measure.get('measure')])
单个xml文件可以正常工作,我有100多个xml文件,需要修改以使用xml文件名在单个csv中写入。