我需要你们一点帮助。
我是编程新手,所以不要对我的代码有太多期望。
事情是这样的,我需要解析文件夹中的一堆 XML 文件并将其写入 .xls 或 .csv 中。到目前为止,我已经解析了 xml 并将其写入 .txt,但我使用它的文件位于程序所在的同一文件夹中。
这是代码:
from xml.dom import minidom
from datetime import *
ano = int(input("Year: "))
mes = int(input("Month: "))
dia = int(input("Day: "))
dt_obj = datetime(ano, mes, dia)
date_str = dt_obj.strftime("%Y-%m-%d")
#Extracting the information from the XML nodes
xmldoc = minidom.parse("NAME OF THE FILE.XML")
NFe = xmldoc.getElementsByTagName("NFe")[0]
infNFe = NFe.getElementsByTagName("infNFe")[0]
ide = infNFe.getElementsByTagName("ide")[0]
nNF = ide.getElementsByTagName("nNF")[0].firstChild.data
dEmi = ide.getElementsByTagName("dEmi")[0].firstChild.data
serie = ide.getElementsByTagName("serie")[0].firstChild.data
emit = infNFe.getElementsByTagName("emit")[0]
cnpj = emit.getElementsByTagName("CNPJ")[0].firstChild.data
nfeProc = xmldoc.getElementsByTagName("nfeProc")[0]
chNFe = nfeProc.getElementsByTagName("chNFe")[0].firstChild.data
try:
# This will create a new file or **overwrite an existing file**.
f = open(date_str+".txt", "w")
try:
f.write("CNPJ: "+cnpj) # Write a string to a file
f.writelines("\nNUMERO DA NOTA: "+nNF)
f.write("\nDATA DE EMISSAO: "+dEmi)
f.write("\nSERIE: "+serie)
f.write("\nCHAVE ELETRONICA: "+chNFe)
finally:
f.close()
except IOError:
pass
我已经成功读取 XML,解析它并从我需要的节点写入信息。
我现在需要的是读取一个包含一堆文件夹并在 .XLS 上书写
任何人?