how do i iterate over an xml and save it into a dataframe in python?

The below code does the job

import xml.etree.ElementTree as ET
import pandas as pd

xml = '''<?xml version="1.0" encoding="UTF-8"?>

root = ET.fromstring(xml)
data = [{'date': e.find('date').text, 'code': e.find('code').text} for e in root.findall('.//trackingevent')]
df = pd.DataFrame(data)


                        date code
0  2020-10-21T11:04:00+01:00   17
1  2020-10-21T08:41:00+01:00   18

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top