ignore an xml file using python?

1

I need help, the code that I have in python reads the xml files inside a predetermined folder, once these files are found it reads them and pulls out the required data inside it, the detail is that I found several xml that contain errors and there is when my program stops, is there a way to ignore those files and send them to another folder? Thanks.

root = 'C:/Users/.../'

fnames = [ arch.name for arch in os.scandir( root ) if(arch.is_file() ) ]

infiles = [open( root + fname, "r", encoding = "utf-8").read()for fname in 
fnames]

columnas = []
for contents in infiles:
soup = BeautifulSoup(contents, 'xml')
Emisores = soup.find('cfdi:Emisor')
Receptor = soup.find('cfdi:Receptor')
Comprobante = soup.find('cfdi:Comproante')
Concepto = soup.find('cfdi:Concepto')
Impuestos = soup.find('cfdi:Traslado')
Timbres = soup.find('tfd:TimbreFiscalDigital')
EmisorNombre = Emisores.get('Nombre')
EmisorRfc = Emisores.get('Rfc')
ReceptorNombre = Receptor.get('Nombre')
ReceptorRfc = Receptor.get('Rfc')
TipoComprobante = Comprobante.get('TipoDeComprobante')
Fecha = Comprobante.get('Fecha')
LugarExpedicion = Comprobante.get('LugarExpedicion')
Serie = Comprobante.get('Serie')    #No todas tienen serie, hacer try 
Folio = Comprobante.get('Folio')
Moneda = Comprobante.get('Moneda')
SubTotal = Comprobante.get('SubTotal')
Descuento = Concepto.get('Descuento')     #No todos tienen descuento, hacer un try



#Iva = Impuestos.get('Importe')

#ImpRetenidos = Impuestos.get('TotalImpuestosRetenidos')    #No todos tienen impuestos retenidos, hacer un try
Total = Comprobante.get('Total')
MetodoPago = Comprobante.get('MetodoPago')
FormaPago = Comprobante.get('FormaPago')
UsoCFDI = Receptor.get('UsoCFDI')
Version = Comprobante.get('Version')
UUID = Timbres.get('UUID')
NoCertificado = Comprobante.get('NoCertificado')

columnas.append([EmisorNombre, EmisorRfc,ReceptorNombre, ReceptorRfc, TipoComprobante, Fecha, LugarExpedicion, Serie, Folio, Moneda, SubTotal, Descuento,
                 Total, MetodoPago, FormaPago, UsoCFDI, Version, UUID, NoCertificado])
    
asked by Eduardo Juarez Benavides 22.10.2018 в 22:33
source

1 answer

0

The easiest thing to do is to use Try

This could be your code:

root = 'C:/Users/.../'

fnames = [ arch.name for arch in os.scandir( root ) if(arch.is_file() ) ]

infiles = [open( root + fname, "r", encoding = "utf-8").read()for fname in 
fnames]

columnas = []

for contents in infiles:
    try: 
        soup = BeautifulSoup(contents, 'xml')
        Emisores = soup.find('cfdi:Emisor')
        Receptor = soup.find('cfdi:Receptor')
        Comprobante = soup.find('cfdi:Comproante')
        Concepto = soup.find('cfdi:Concepto')
        Impuestos = soup.find('cfdi:Traslado')
        Timbres = soup.find('tfd:TimbreFiscalDigital')
        EmisorNombre = Emisores.get('Nombre')
        EmisorRfc = Emisores.get('Rfc')
        ReceptorNombre = Receptor.get('Nombre')
        ReceptorRfc = Receptor.get('Rfc')
        TipoComprobante = Comprobante.get('TipoDeComprobante')
        Fecha = Comprobante.get('Fecha')
        LugarExpedicion = Comprobante.get('LugarExpedicion')
        Serie = Comprobante.get('Serie')    #No todas tienen serie, hacer try 
        Folio = Comprobante.get('Folio')
        Moneda = Comprobante.get('Moneda')
        SubTotal = Comprobante.get('SubTotal')
        Descuento = Concepto.get('Descuento')     #No todos tienen descuento, hacer un try

        #Iva = Impuestos.get('Importe')

        #ImpRetenidos = Impuestos.get('TotalImpuestosRetenidos')    #No todos tienen impuestos retenidos, hacer un try
        Total = Comprobante.get('Total')
        MetodoPago = Comprobante.get('MetodoPago')
        FormaPago = Comprobante.get('FormaPago')
        UsoCFDI = Receptor.get('UsoCFDI')
        Version = Comprobante.get('Version')
        UUID = Timbres.get('UUID')
        NoCertificado = Comprobante.get('NoCertificado')

        columnas.append([EmisorNombre, EmisorRfc,ReceptorNombre, ReceptorRfc, TipoComprobante, Fecha, LugarExpedicion, Serie, Folio, Moneda, SubTotal, Descuento,
                         Total, MetodoPago, FormaPago, UsoCFDI, Version, UUID, NoCertificado])

    except:
        pass
    
answered by 22.10.2018 / 22:45
source