我正在尝试将嵌套的xml从在线站点转换为csv以进行练习。 但是我无法继续使用该方法。对于简单的xml,我能够进行转换。这是在线here
中的xml我尝试过此方法,但没有得到预期的答案。
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')
我仅需要csv中的此列。 开始日期,温度露点,温度风寒,持续,总计,浮动,相对,风,每小时温度,阵风,浮动。