首页 > 解决方案 > 解析xml文件的目录并将数据封装在多个数组中

问题描述

我发现了一个问题,我需要解析更多的 XML 文件,并为每个文件将数据封装在数组数组中。现在,使用我编写的代码,我能够解析文件并将所有解析的数据插入到单个数组中。

for filename in filenames:  
tree = ET.parse(filename)
root = tree.getroot()

#Per estrarre dati diversi basta cambiare l'entita PADRE nei vari cicli for
for DatiGeneraliDocumento in root.iter('DatiGeneraliDocumento'):
    for FiglioDatiGeneraliDocumento in DatiGeneraliDocumento:
        if(FiglioDatiGeneraliDocumento.tag=='TipoDocumento'):
            if(FiglioDatiGeneraliDocumento.text=='TD04'):
                Concatenazione=FiglioDatiGeneraliDocumento.text + str('(NotaCredito)')
                PrimoFor.append(Concatenazione)
            else:
                Concatenazione=FiglioDatiGeneraliDocumento.text + str('(fattura)')
                PrimoFor.append(Concatenazione)
for CedentePrestatore in root.iter('CedentePrestatore'):
    for TagFiglioCedentePrestatore in CedentePrestatore:
            for TagNipoteCedentePrestatore in TagFiglioCedentePrestatore:
                for ProNipoteCedentePrestatore in TagNipoteCedentePrestatore:
                    if(ProNipoteCedentePrestatore.tag=='Denominazione'):
                        PrimoFor.append(ProNipoteCedentePrestatore.text)
for ImportoTotaleDocumento in root.iter('ImportoTotaleDocumento'):
    PrimoFor.append(ImportoTotaleDocumento.text)
    data=[PrimoFor]
    print(data)

结果如下。

[['TD01(fattura)', 'Neoss Italia Srl', '2255.48', 'TD01(fattura)', 'ASTIDENTAL DI SABBIONE SPA', '2717.60']]

我需要这个。

[['TD01(fattura)', 'Neoss Italia Srl','2255.48'],['TD01(fattura)', 'ASTIDENTAL DI SABBIONE SPA', '2717.60']]

谁能帮帮我谢谢...

标签: pythonlistxml-parsing

解决方案


推荐阅读