我在解析多个xml文件并将其作为Python中的数据帧处理时遇到麻烦 [英] I trouble in how do parse multiple xml file and process it as dataframe in Python

查看:56
本文介绍了我在解析多个xml文件并将其作为Python中的数据帧处理时遇到麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将多个xml文件解析为数据框。有相同的xpath。

I want parse multi xml file into dataframe. There are same xpath.

我使用了元素树和os Python库,它可以解析所有文件,但可以打印出空的数据框。但是,如果代码中没有多个文件,则可以正常工作。

I have used element tree and os Python library.It can parse all the files, but it print out empty dataframe. However if code without multiple file, it can work properly.

mypath = r'C:\Users\testFile'
files = [path.join(mypath, f) for f in listdir(mypath) if f.endswith('.xml')]

for file in files:
    xtree = et.parse(file)
    xroot = xtree.getroot()
    df_cols=['value']
    out_xml=pd.DataFrame(columns=df_cols)
    for node in xroot.findall(r'./Group[1]/Details/Section[3]/Subreport/Group/Group[1]/Details/Section/Field'):
        name = node.attrib.get('Name')
        value = node.find('Value').text
        out_xml = out_xml.append(pd.Series([value],index=df_cols),ignore_index=True)
    df = pd.DataFrame(np.reshape(out_xml.values, (-1, 4)))


推荐答案

如果您需要一个包含所有数据的数据框,则需要将每个数据框连接到一个主数据框

If you need a single dataframe with all data,you need to concat each dataframe to one main dataframe

mypath = r'C:\testFile'
files = [path.join(mypath, f) for f in listdir(mypath) if f.endswith('.xml')]

mainDF = pd.DataFrame()
for file in files:
    xtree = et.parse(file)
    xroot = xtree.getroot()
    df_cols=['value']
    out_xml=pd.DataFrame(columns=df_cols)
    for node in xroot.findall(r'./Group[1]/Details/Section[3]/Subreport/Group/Group[1]/Details/Section/Field'):
        name = node.attrib.get('Name')
        value = node.find('Value').text
        out_xml = out_xml.append(pd.Series([value],index=df_cols),ignore_index=True)
    df = pd.DataFrame(np.reshape(out_xml.values, (-1, 4)))
    mainDF = pd.concat([mainDF,df])
 mainDF.to_csv("filename.csv")

这篇关于我在解析多个xml文件并将其作为Python中的数据帧处理时遇到麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆