如何将命名空间嵌套的xml转换为csv?

时间:2019-02-07 08:05:25

标签: python xml csv namespaces

我正在尝试将嵌套的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中的此列。 开始日期,温度露点,温度风寒,持续,总计,浮动,相对,风,每小时温度,阵风,浮动。

0 个答案:

没有答案