我有XML文件:https://drive.google.com/file/d/1DXGHFuM_48oSPpUDrqz3DxfyzvAgzOTD/view,想转换为数据框
我写了下面的脚本来将子级/子级子转换为数据框
import pandas as pd
import xml.etree.ElementTree as ET
XML_File_Path = r"Local File Path"
# Create element tree object
Tree = ET.parse(XML_File_Path)
# Get root element
Root = Tree.getroot()
def Root_DF(Node, Order):
DF = pd.DataFrame([Node.attrib])
DF['Order'] = Order
return DF
Node_DF = pd.DataFrame()
Order = 1
for Node in Root[1]:
Node_DF = Node_DF.append(Root_DF(Node, Order), sort=True)
Order +=1
for Node_C1 in Node:
Node_DF = Node_DF.append(Root_DF(Node_C1, Order), sort=True)
Order +=1
for Node_C2 in Node_C1:
Node_DF = Node_DF.append(Root_DF(Node_C2, Order), sort=True)
Order +=1
但是有嵌套/深子对象存在,如何简化脚本以将所有嵌套标签转换为数据框? 为每个孩子编写For Loop不是明智的方法,请为此提供帮助。 我希望代码可以在任何级别上工作。