PYTHON解析目录中的多个文件XML并将数据上传到CSV文件中

时间:2019-03-19 14:10:36

标签: python xml csv xml-parsing

我有一个问题,我应该从多个XML文件中提取数据并将其上传到CSV ...当涉及到单个XML文件时,我可以提取并以CSV格式加载数据,但是当它是XML文件的目录时只能看到名称,但在传递参数时什么也没有发生。我附上了代码,请帮助我。

import csv 
import xml.etree.ElementTree as ET
import os


path = r"C:\\Users\ADMIN\Desktop\prog"
string = []


for filename in os.listdir(path):
    if not filename.endswith('.xml'):continue
    fullname=os.path.join(path,filename)
    print(fullname)
    string.append(fullname)



tree= ET.parse(fullname)
root = tree.getroot()


csvfile=open('prova.csv','w')
csv_writer = csv.writer(csvfile)


PrimoFor=[]
SecondoFor=[] 
TerzoFor=[]
QuartoFor=[]


print("Dati Riepilogo per aliquota IVA e natura")
for datir in root.iter('DatiRiepilogo'):
        for element in datir:
            print(element.tag,element.text)
            PrimoFor.append(element.text)

for CedentePrestatore in root.iter('CedentePrestatore'):
    for TagFiglioCedentePrestatore in CedentePrestatore:
            for TagNipoteCedentePrestatore in TagFiglioCedentePrestatore:
                for ProNipoteCedentePrestatore in TagNipoteCedentePrestatore:
                    print(ProNipoteCedentePrestatore.tag,ProNipoteCedentePrestatore.text)
                    PrimoFor.append(ProNipoteCedentePrestatore.text)
for DatiGeneraliDocumento in root.iter('DatiGeneraliDocumento'):
    for FiglioDatiGeneraliDocumento in DatiGeneraliDocumento:
        if(FiglioDatiGeneraliDocumento.tag!='Divisa'):
            print(FiglioDatiGeneraliDocumento.tag,FiglioDatiGeneraliDocumento.text)
            PrimoFor.append(FiglioDatiGeneraliDocumento.text)
for DatiPagamento in root.iter('DatiPagamento'):
    for TagFiglioDatiPagamento in DatiPagamento:
        for TagNipoteDatiPagamento in TagFiglioDatiPagamento:
            if(TagNipoteDatiPagamento.tag=='ModalitaPagamento'):
                print(TagNipoteDatiPagamento.tag,TagNipoteDatiPagamento.text)
                PrimoFor.append(TagNipoteDatiPagamento.text)

csv_writer.writerow(PrimoFor)


        #closecsv 
csvfile.close()

我也尝试了此功能,但它没有任何改变

for path , dirs, files in os.walk(path):
    for filename in files:
        print(filename)
        string.append(filename)

先谢谢了。 抱歉,该语言是Google翻译

1 个答案:

答案 0 :(得分:0)

(注意:以下代码由于没有测试数据而未经测试)

尝试以下代码。如您所说,您要为目录中的所有XML文件重复读取XML文件。缺少的是重复读取每个文件。我进一步假设结果应该写入相同的CSV文件中。另外,我重命名了包含文件名列表的变量。这不是严格必要的,但是最好避免使用与关键字相同的名称来命名变量。

import csv 
import xml.etree.ElementTree as ET
import os


path = r"C:\\Users\ADMIN\Desktop\prog"
filenames = []

for filename in os.listdir(path):
    if not filename.endswith('.xml'):
        continue
    fullname = os.path.join(path,filename)
    print(fullname)
    filenames.append(fullname)

csvfile = open('prova.csv','w')
csv_writer = csv.writer(csvfile)

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

    PrimoFor=[]

    print("Dati Riepilogo per aliquota IVA e natura")
    for datir in root.iter('DatiRiepilogo'):
            for element in datir:
                print(element.tag,element.text)
                PrimoFor.append(element.text)

    for CedentePrestatore in root.iter('CedentePrestatore'):
        for TagFiglioCedentePrestatore in CedentePrestatore:
                for TagNipoteCedentePrestatore in TagFiglioCedentePrestatore:
                    for ProNipoteCedentePrestatore in TagNipoteCedentePrestatore:
                        print(ProNipoteCedentePrestatore.tag,ProNipoteCedentePrestatore.text)
                        PrimoFor.append(ProNipoteCedentePrestatore.text)
    for DatiGeneraliDocumento in root.iter('DatiGeneraliDocumento'):
        for FiglioDatiGeneraliDocumento in DatiGeneraliDocumento:
            if(FiglioDatiGeneraliDocumento.tag!='Divisa'):
                print(FiglioDatiGeneraliDocumento.tag,FiglioDatiGeneraliDocumento.text)
                PrimoFor.append(FiglioDatiGeneraliDocumento.text)
    for DatiPagamento in root.iter('DatiPagamento'):
        for TagFiglioDatiPagamento in DatiPagamento:
            for TagNipoteDatiPagamento in TagFiglioDatiPagamento:
                if(TagNipoteDatiPagamento.tag=='ModalitaPagamento'):
                    print(TagNipoteDatiPagamento.tag,TagNipoteDatiPagamento.text)
                    PrimoFor.append(TagNipoteDatiPagamento.text)

    csv_writer.writerow(PrimoFor)

#closecsv 
csvfile.close()