I am looking to turn this XNL file countries into a table in a CSV. However, I am having trouble parsing it/extracting the data from it. I am trying to turn it into a table with 5 columns which are
['CtryNm', 'CcyNm', 'Ccy', 'CcyNbr', 'CcyMnrUnts']
This is a snippet of how the xml file is structured
<ISO_4217 Pblshd="2024-01-01">
<CcyTbl>
<CcyNtry>
<CtryNm>AFGHANISTAN</CtryNm>
<CcyNm>Afghani</CcyNm>
<Ccy>AFN</Ccy>
<CcyNbr>971</CcyNbr>
<CcyMnrUnts>2</CcyMnrUnts>
</CcyNtry>
<CcyNtry>
<CtryNm>ZZ07_No_Currency</CtryNm>
<CcyNm>The codes assigned for transactions where no currency is involved</CcyNm>
<Ccy>XXX</Ccy>
<CcyNbr>999</CcyNbr>
<CcyMnrUnts>N.A.</CcyMnrUnts>
</CcyNtry>
</CcyTbl>
</ISO_4217>
I tried doing
def parse_xml(xml_file):
tree = ET.parse(xml_file)
root = tree.getroot()
return root
def extract_data(root):
data = []
for record in root.findall('CcyNtry'):
row = {}
for field in record:
row[field.tag] = field.text
data.append(row)
return data
def main(xml_file, csv_file):
root = parse_xml(xml_file)
data = extract_data(root)
df = to_dataframe(data)
to_csv(df, csv_file)
main(countries, 'output.csv')
However this just seems to return an empty file. Anyone know what I'm doing wrong here? Or is there just a simple way to turn the data from this XML into a dataframe?