PYTHON解析位于目录中的多个XML文件,并将数据上载到CSV文件中
我有一个问题,我应该提取多个XML文件的数据,并上传到CSV。。。当涉及单个XML文件时,我可以提取并加载CSV格式的数据,但当它是XML文件目录时,我只能看到名称,但当参数通过时,什么也看不到。随信附上密码,请帮助我PYTHON解析位于目录中的多个XML文件,并将数据上载到CSV文件中,python,xml,csv,xml-parsing,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(p
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 translate(注:由于没有测试数据,以下代码未经测试)
请尝试以下代码。如前所述,您希望对目录中的所有XML文件重复读取XML文件。缺少的是对每个文件重复读取。我进一步假设结果应该写入同一个CSV文件。此外,我重命名了包含文件名列表的变量。这并不是严格必要的,但最好避免将变量命名为与关键字相同的名称
导入csv
将xml.etree.ElementTree作为ET导入
导入操作系统
path=r“C:\\Users\ADMIN\Desktop\prog”
文件名=[]
对于os.listdir(路径)中的文件名:
如果不是filename.endswith('.xml'):
持续
fullname=os.path.join(路径,文件名)
打印(全名)
filenames.append(全名)
csvfile=open('prova.csv','w')
csv\u writer=csv.writer(csvfile)
对于文件名中的文件名:
tree=ET.parse(文件名)
root=tree.getroot()
原稿=[]
印刷品(“Dati Riepilogo per aliquota IVA e natura”)
对于根目录中的datir.iter('DatiRiepilogo'):
对于datir中的元素:
打印(element.tag、element.text)
PrimoFor.append(element.text)
对于root.iter中的CedentePrestatore(“CedentePrestatore”):
对于CedentePrestatore的TagFiglioCedentePrestatore:
对于TagfigiocedentePrestore中的TagnipotedentePrestore:
对于位于TagNipoteCedentePrestatore的ProNipoteCedentePrestatore:
打印(ProNipoteCedentePrestatore.tag、ProNipoteCedentePrestatore.text)
append(ProNipoteCedentePrestatore.text)
对于根目录中的DatiGeneraliDocumento.iter('DatiGeneraliDocumento'):
对于DatiGeneraliDocumento中的FiglioDatiGeneraliDocumento:
如果(FiglioDatiGeneraliDocumento.tag!='divia'):
打印(FiglioDatiGeneraliDocumento.tag、FiglioDatiGeneraliDocumento.text)
append(FiglioDatiGeneraliDocumento.text)
对于根.iter('DatiPagamento')中的DatiPagamento:
对于DatiPagamento的TagFiglioDatiPagamento:
对于TagFiglioDatiPagamento中的TagNipoteDatiPagamento:
如果(TagNipoteDatiPagamento.tag=='ModalitaPagamento'):
打印(TagNipoteDatiPagamento.tag,TagNipoteDatiPagamento.text)
append(TagNipoteDatiPagamento.text)
csv_writer.writerow(原稿)
#closecsv
csvfile.close()
(注意:由于没有测试数据,以下代码未经测试)
请尝试以下代码。如前所述,您希望对目录中的所有XML文件重复读取XML文件。缺少的是对每个文件重复读取。我进一步假设结果应该写入同一个CSV文件。此外,我重命名了包含文件名列表的变量。这并不是严格必要的,但最好避免将变量命名为与关键字相同的名称
导入csv
将xml.etree.ElementTree作为ET导入
导入操作系统
path=r“C:\\Users\ADMIN\Desktop\prog”
文件名=[]
对于os.listdir(路径)中的文件名:
如果不是filename.endswith('.xml'):
持续
fullname=os.path.join(路径,文件名)
打印(全名)
filenames.append(全名)
csvfile=open('prova.csv','w')
csv\u writer=csv.writer(csvfile)
对于文件名中的文件名:
tree=ET.parse(文件名)
root=tree.getroot()
原稿=[]
印刷品(“Dati Riepilogo per aliquota IVA e natura”)
对于根目录中的datir.iter('DatiRiepilogo'):
对于datir中的元素:
打印(element.tag、element.text)
PrimoFor.append(element.text)
对于root.iter中的CedentePrestatore(“CedentePrestatore”):
对于CedentePrestatore的TagFiglioCedentePrestatore:
对于TagfigiocedentePrestore中的TagnipotedentePrestore:
对于位于TagNipoteCedentePrestatore的ProNipoteCedentePrestatore:
打印(ProNipoteCedentePrestatore.tag、ProNipoteCedentePrestatore.text)
append(ProNipoteCedentePrestatore.text)
对于根目录中的DatiGeneraliDocumento.iter('DatiGeneraliDocumento'):
对于DatiGeneraliDocumento中的FiglioDatiGeneraliDocumento:
如果(FiglioDatiGeneraliDocumento.tag!='divia'):
打印(FiglioDatiGeneraliDocumento.tag、FiglioDatiGeneraliDocumento.text)
append(FiglioDatiGeneraliDocumento.text)
对于根.iter('DatiPagamento')中的DatiPagamento:
对于DatiPagamento的TagFiglioDatiPagamento:
对于TagFiglioDatiPagamento中的TagNipoteDatiPagamento:
如果(TagNipoteDatiPagamento.tag=='ModalitaPagamento'):
打印(TagNipoteDatiPagamento.tag,TagNipoteDatiPagamento.text)
append(TagNipoteDatiPagamento.text)
csv_writer.writerow(原稿)
#closecsv
csvfile.close()
将目录中所有XML文件的完整路径放入名为string
的列表中,然后尝试从名为fullname
的变量中定义的路径的文件加载数据。但是,该变量是该变量上方for循环的局部变量。该变量的值应该被认为是未定义的,python实际上应该警告您这一点。是否只处理第一个(或最后一个)文件?或者您想为每个文件重复下面的所有代码?谢谢您的回答。如果我把{string}或{fullname}un