I have been trying to scrape an XML file to copy content from 2 tags, Code and Source only. The xml file looks as follows:
<Series xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<RunDate>2018-06-12</RunDate>
<Instruments>
<Instrument>
<Code>27BA1</Code>
<Source>YYY</Source>
</Instrument>
<Instrument>
<Code>28BA1</Code>
<Source>XXX</Source>
</Instrument>
<Code>29BA1</Code>
<Source>XXX</Source>
</Instrument>
<Code>30BA1</Code>
<Source>DDD</Source>
</Instrument>
</Instruments>
</Series>
I'm only getting it right to scrape the first code. Below is the code. Can anyone help?
import xml.etree.ElementTree as ET
import csv
tree = ET.parse("data.xml")
csv_fname = "data.csv"
root = tree.getroot()
f = open(csv_fname, 'w')
csvwriter = csv.writer(f)
count = 0
head = ['Code', 'Source']
csvwriter.writerow(head)
for time in root.findall('Instruments'):
row = []
job_name = time.find('Instrument').find('Code').text
row.append(job_name)
job_name_1 = time.find('Instrument').find('Source').text
row.append(job_name_1)
csvwriter.writerow(row)
f.close()