在Python中将多个嵌套XML解析为Panda数据帧表

在Python中将多个嵌套XML解析为Panda数据帧表,python,xml,parsing,Python,Xml,Parsing,问:我正试图使用XML.etree.cElementTree库将XML解析为Python中的数据帧。但是如何在一行中生成结果,包括a和b,因此将是“1,2,3,a,4,5,6,b”。 谢谢大家! 您可以使用其他库吗 TestFile="test.xml" ttree = etree.parse(TestFile) troot = ttree.getroot() df_cols =["Col1", "Col2", "Col3&q

问:我正试图使用XML.etree.cElementTree库将XML解析为Python中的数据帧。但是如何在一行中生成结果,包括a和b,因此将是“1,2,3,a,4,5,6,b”。
谢谢大家!

您可以使用其他库吗

TestFile="test.xml"
ttree = etree.parse(TestFile)
troot = ttree.getroot()
df_cols =["Col1", "Col2", "Col3", "Col4","Col5","Col6"
              "Col7", "Col8"]
df = pd.DataFrame(columns =df_cols)

for i in troot: 
    df = df.append(pd.Series([i.get('A'), i.get('B'),i.get('C'), i.get('D'),
                                     i.get('E'), i.get('F'),i.get('G'),i.get('H')],
                          index = df_cols), ignore_index=True)
        
df.head()

那么XML代表数据帧中的一行?如果是,您可以扩展以显示多行吗?
TestFile="test.xml"
ttree = etree.parse(TestFile)
troot = ttree.getroot()
df_cols =["Col1", "Col2", "Col3", "Col4","Col5","Col6"
              "Col7", "Col8"]
df = pd.DataFrame(columns =df_cols)

for i in troot: 
    df = df.append(pd.Series([i.get('A'), i.get('B'),i.get('C'), i.get('D'),
                                     i.get('E'), i.get('F'),i.get('G'),i.get('H')],
                          index = df_cols), ignore_index=True)
        
df.head()
from simplified_scrapy import SimplifiedDoc

html = '''
<?xml version='1.0' encoding='UTF-8' ?>
  <DOC>
    <INFO1
      A = "1"
      B = "2"
      C = "3"
    >
      <INFO12
        D = "a"
      >
      </INFO12>
    </INFO1>
    <INFO2
      E = "4"
      F = "5"
      G = "6"
    >
      <INFO21
        H = "b"
      >
      </INFO21>
    </INFO2>
 </DOC>
'''
doc = SimplifiedDoc(html)
infos = doc.DOC.children
row = [infos[0].A,infos[0].B,infos[0].C,infos[0].child.D,
    infos[1].E,infos[1].F,infos[1].G,infos[1].child.H]
print (row)
['1', '2', '3', 'a', '4', '5', '6', 'b']