Python 解析XML并将其转换为数据帧的最佳方法

Python 解析XML并将其转换为数据帧的最佳方法,python,pandas,xml-parsing,Python,Pandas,Xml Parsing,我有以下XML(这是一个示例): 但我得到的只是:{}日期{} 如果我在同一个循环中更深入地使用它 for child in child: print child.tag, child.attrib 我知道:{}日期{'valeur':'14032019'} 而且它会一直持续下去。我建议在lxml阅读器上使用BeautifulSoup(如果我正确理解了您的请求): 可能重复我尝试了该解决方案,但无法使其适应我的文件您尝试了什么,结果如何? import xml.etree.Elemen

我有以下XML(这是一个示例):

但我得到的只是:{}日期{}

如果我在同一个循环中更深入地使用它

for child in child:
    print child.tag, child.attrib
我知道:{}日期{'valeur':'14032019'}


而且它会一直持续下去。

我建议在
lxml
阅读器上使用
BeautifulSoup
(如果我正确理解了您的请求):


可能重复我尝试了该解决方案,但无法使其适应我的文件您尝试了什么,结果如何?
import xml.etree.ElementTree as ET
tree = ET.parse('testtest.xml')
root = tree.getroot()

for child in root:
    print child.tag, child.attrib
for child in child:
    print child.tag, child.attrib
from bs4 import BeautifulSoup
import pandas as pd

xml=b"""\
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<HDR_DONNEES xmlns="http://ERABLE_HDR.com/ns1">
    <Dates>
        <Date valeur="14032019">
            <Depart ACR_DepartHTA="BDX" ACR_PosteSource="BDX" GdoDepart="V.LOTC0018" Nom_DepartHTA="BOURLANG" PS_DepartHTA="V.LOT" NomPosteSource="VILLELOT">
                <M H="1150" UTM="20850" ITM="94" IFg="0" UNB="1" INB="1"/>
            </Depart>
            <Depart ACR_DepartHTA="BDX" ACR_PosteSource="BDX" GdoDepart="V.LOTC0005" Nom_DepartHTA="MARCHE G" PS_DepartHTA="V.LOT" NomPosteSource="VILLELOT">
                <M H="1150" UTM="20850" ITM="41" IFg="0" UNB="1" INB="1"/>
            </Depart>
            <Depart ACR_DepartHTA="NTS" ACR_PosteSource="NTS" GdoDepart="PALLUC2703" Nom_DepartHTA="FROIDFON" PS_DepartHTA="PALLU" NomPosteSource="PALLUAU">
                <M H="1140" UTM="0" ITM="0" IFg="100" UNB="0" INB="1"/>
            </Depart>
        </Date>
    </Dates>
</HDR_DONNEES>"""


soup = BeautifulSoup(xml,features="lxml")

data={}
for i,depart in enumerate(soup.find_all('depart')):
    data[i]=depart.attrs
    for m in depart.findChildren():
        data[i]['m']=list(m.attrs.values())

df=pd.DataFrame.from_dict(data, orient='index')
print(df)
  acr_departhta acr_postesource   gdodepart nom_departhta ps_departhta nompostesource                           m
0           BDX             BDX  V.LOTC0018      BOURLANG        V.LOT       VILLELOT  [1150, 20850, 94, 0, 1, 1]
1           BDX             BDX  V.LOTC0005      MARCHE G        V.LOT       VILLELOT  [1150, 20850, 41, 0, 1, 1]
2           NTS             NTS  PALLUC2703      FROIDFON        PALLU        PALLUAU     [1140, 0, 0, 100, 0, 1]