【问题标题】:PYTHON Parsing multiple files XML situated in directory and upload data in a CSV filePYTHON解析位于目录中的多个文件XML并将数据上传到CSV文件中
【发布时间】:2019-08-10 02:21:02
【问题描述】:

我有一个问题,我应该从多个 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)

提前致谢。 抱歉,语言是谷歌翻译

【问题讨论】:

  • 您将目录中所有 XML 文件的完整路径放入名为 string 的列表中,但随后您尝试从文件中加载数据,该文件的路径在名为 fullname 的变量中定义.但是,该变量对于上面的 for 循环是局部的。该变量的值应该被认为是未定义的,python实际上应该警告你。您只想使用第一个(或最后一个)文件吗?或者您想为每个文件重复以下所有代码?
  • 感谢您的回答。如果我将 {string} 或 {fullname } 放在 {ET.parse() } 下,则没有任何变化。我希望为每个文件重复下面的代码......也就是说,从所有 XML 文件中提取数据

标签: python xml csv xml-parsing


【解决方案1】:

(注:以下代码因没有测试数据未测试)

试试下面的代码。正如您所说,您希望为目录中的所有 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()

【讨论】:

  • 我不知道你是谁,但我爱你 :D ... 非常感谢。我知道我必须做一个循环,但我不知道把它放在哪里。现在它只适用于跳过 csv 的一行。第一个文件的数据放在第一行,而不是第二个文件的数据放在第三行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-26
  • 1970-01-01
  • 2019-11-15
  • 2020-04-15
相关资源
最近更新 更多