2017-10-18 128 views
1

我想解析一個XML文件,我被封鎖了。需要幫助來解析一個XML文件

拿上我的XML文件中快速瀏覽一下:

<editrust> 
    <flux ref='ITFR2006' sens='IN'> 
    <intervalle ref='H10'> 
     <terminé>1</terminé> 
     <prisEnComtpe>1</prisEnComtpe> 
    </intervalle> 
    <intervalle ref='H60'> 
     <terminé>11</terminé> 
     <prisEnComtpe>11</prisEnComtpe> 
    </intervalle> 
    <intervalle ref='D1'> 
     <terminé>150</terminé> 
     <prisEnComtpe>150</prisEnComtpe> 
    </intervalle> 
    <intervalle ref='D2'> 
     <terminé>150</terminé> 
     <prisEnComtpe>150</prisEnComtpe> 
    </intervalle> 
    </flux> 

    <flux ref='ITFR2007_2021' sens='IN'> 
    <intervalle ref='H10'> 
     <terminé>2</terminé> 
     <prisEnComtpe>2</prisEnComtpe> 
    </intervalle> 
    <intervalle ref='H60'> 
     <terminé>181</terminé> 
     <prisEnComtpe>121</prisEnComtpe> 
    </intervalle> 
    <intervalle ref='D1'> 
     <terminé>600</terminé> 
     <prisEnComtpe>600</prisEnComtpe> 
    </intervalle> 
    <intervalle ref='D2'> 
     <terminé>600</terminé> 
     <prisEnComtpe>600</prisEnComtpe> 
    </intervalle> 
    </flux> 
... 

我想呈現像一本字典列表

{'ITFR2006': ['IN', 'H10', '1','1', 'H60', '11', '11', 'D1', '150', '150'],... 

我做了一個腳本:

import xml.etree.ElementTree as etree 
tree = etree.parse('fichier.xml') 
root = tree.getroot() 

flux = {} 

def findText(node): 

    for child in node: 

     if child.attrib.get("ref"): 

      if "ITFR" in child.attrib.get("ref"): 
       itfr = child.attrib.get("ref") 
       flux[itfr] = [] 

       print("\n-----------------\n") 

      print(child.attrib.get("ref")) 

     if child.attrib.get("sens"): 
      flux[itfr].append(child.attrib.get("sens")) 
      print(child.attrib.get("sens")) 

     if child.text.strip(): 

      print(child.text.strip()) 

     findText(child) 


findText(root) 

print(flux) 

腳本有這種渲染:

----------------- 

ITFR2006 
IN 
H10 
1 
1 
H60 
11 
11 
D1 
150 
150 
D2 
150 
150 

----------------- 

ITFR2007_2021 
IN 
H10 
2 
2 
H60 
181 
121 
D1 
600 
600 
D2 
600 
600 
.... 

所以,print(flux)使得:

{'ITFR2006': ['IN'], 'ITFR2007_2021': ['IN'], 'ITFR2008': ['IN'], 'ITFR2011_2020': ['IN'], 'ITFR2012': ['OUT'], 'ITFR2013': ['OUT'], 'ITFR2014': ['OUT'], 'ITFR2017': ['OUT'], 'ITFR2018': ['OUT'], 'ITFR2019': ['OUT'], 'ITFR2023': ['OUT'], 'ITFR2024': ['OUT']} 

這是一個很好的begining我想,但我不能與其他值( 'H10',填寫我的清單 '1', '1',' H60',...)

任何想法完成我的工作?

感謝

+0

你真的需要解釋的結果,你想擁有什麼結束。 – Tomalak

+0

我想要一些類似{'ITFR2006':['IN','H10','1','1','H60','11','11','D1','150','150' ],...謝謝 – GrandGTO

回答

1

這裏是一個辦法做到這一點(測試使用Python 3.6):

import xml.etree.ElementTree as etree 
import pprint 

tree = etree.parse('fichier.xml') 
fluxdict = {} 

for flux in tree.findall("flux"): 
    # The key 
    key = flux.get("ref") 
    # Add first item to the list 
    val = [flux.get("sens")] 

    for intervalle in flux.findall("intervalle"): 
     ref = intervalle.get("ref") 
     termine = intervalle.findtext("terminé") 
     prisEnComtpe = intervalle.findtext("prisEnComtpe") 

     # Add items by extending list 
     val.extend([ref, termine, prisEnComtpe]) 

    # Add key:val pair for this 'flux' 
    fluxdict[key] = val 

pprint.pprint(fluxdict) 

輸出:

{'ITFR2006': ['IN', 
       'H10', 
       '1', 
       '1', 
       'H60', 
       '11', 
       '11', 
       'D1', 
       '150', 
       '150', 
       'D2', 
       '150', 
       '150'], 
'ITFR2007_2021': ['IN', 
        'H10', 
        '2', 
        '2', 
        'H60', 
        '181', 
        '121', 
        'D1', 
        '600', 
        '600', 
        'D2', 
        '600', 
        '600']} 
+0

這正是我想要的!非常感謝你 ! – GrandGTO