在Python中,我很难解析多个xml文件并将其作为数据帧处理

在Python中,我很难解析多个xml文件并将其作为数据帧处理,python,xml,elementtree,Python,Xml,Elementtree,我想将多xml文件解析为数据帧。有相同的xpath 我使用了元素树和os Python库。它可以解析所有的文件,但它打印出空的数据帧。但是,如果代码没有多个文件,它可以正常工作 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) xr

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

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

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)))

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

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")

你想要一个包含所有数据的最终数据帧吗?谢谢你的回复。是的,我想要所有的数据帧。但它打印出空的数据框。检查我的回答,但在我mainDF.to_csv(r'C:\test\1.csv',index=None,header=['fre','status','a','b'])或print(mainDF)之后,它什么也没有检查您的
df
dataframe是否已正确实现..检查它是否为dataframe且是否存在预期值..并在for循环后将mainDF保存到csv。如果没有数据,则无法再现问题。谢谢,但它仅输出一个文件数据。另一个文件输出不见了。@Jovisch我已经更新了答案,这就是你保存文件的方式吗?是的,我也使用了这个(mainDF.to_csv),但不起作用。我现在只能得到一个文件的值。但问题是无法将大小为570846的阵列重塑为形状(4)