Saturday, 27 September 2014

How to open this XML file to create dataframe in Python?



Does anyone have a suggestion for the best way to open the xml data on the site below to put it in a dataframe (I prefer working with pandas) in python? The file is on the "Data - XML (sdmx/zip)" link on this site:


http://ift.tt/1aVsakW


I've tried using the following by copying from http://ift.tt/1BmgZwe, and it seems I'm getting close:



from lxml import objectify
import pandas as pd

path = 'feds200628.xml'
xml = objectify.parse(open(path))
root = xml.getroot()
root.getchildren()[0].getchildren()
df = pd.DataFrame(columns=('id', 'name'))

for i in range(0,4):
obj = root.getchildren()[i].getchildren()
row = dict(zip(['id', 'name'], [obj[0].text, obj[1].text]))
row_s = pd.Series(row)
row_s.name = i
df = df.append(row_s)


Still, I don't know enough about xml to get me the rest of the way.


Any help would be awesome - I don't even need it to be in a dataframe, I just need to figure out how to parse this content in python somehow.


No comments:

Post a Comment