I am trying out to convert the nested xml from online site to csv for practicing. But I'm not able to proceed with the approach. For simple xml I was able to convert. This is the xml from online here
I tried with this, but it did not yield expected answer.
import pandas as pd
import xml.etree.cElementTree as et
parsedXML = et.parse(r'./Desktop/MapClick.xml')
dfcols = ['start_valid_time','dew_point','wind_chill','sustained','total','floating','relative','wind','hourly','gust','floating']
df = pd.DataFrame(columns=dfcols)
def getvalueofnode( node ):
return node.text if node is not None else None
for node in parsedXML.getroot():
start_valid_time = node.find('time-layout/start-valid-time')
dew_point = node.find('parameters/temperature1/value')
wind_chill = node.find('parameters/temperature2/value')
sustained = node.find('parameters/wind-speed/value')
total = node.find('parameters/hourly-qpf/value')
floating = node.find('parameters/cloud-amount/value')
relative = node.find('parameters/probability-of-precipitation/value')
wind = node.find('parameters/humidity/value')
hourly = node.find('parameters/direction/value')
gust = node.find('parameters/temperature3/value')
floating = node.find('parameters/wind-speed/value')
df = df.append( pd.Series(
[start_valid_time, getvalueofnode(dew_point), getvalueofnode(wind_chill), getvalueofnode(sustained),getvalueofnode(total), getvalueofnode(floating), getvalueofnode(relative),getvalueofnode(wind),getvalueofnode(hourly), getvalueofnode(gust),getvalueofnode(floating)],
index=dfcols) ,ignore_index=True)
df.to_csv('./Desktop/MapClick.csv')
I need only this columns in csv. Start_valid_date, temperature-dew point, temperature-wind chill, sustained, total, floating, relative, wind, temperature-hourly, gust, floating.