Python attribute error in Split

0

Someone knows why it appears in the following code:

  

ERROR in line 6 "tuple" object has no attribute 'split'

The code is as follows:

import time

with open('FicheroEntrada.csv') as infile1, open('Diferencias.txt', 'w') as outfile:
    for line1 in zip(infile1):
        #Busco en el fichero la línea que me interesa.
        if line1.split(";")[4] == "Valor Byte Semaforo":
            #Después de 5 segundos voy a comprobar como esta el valor en el fichero de salida.
            time.sleep(2)
            #Comparo el valor del fichero de entrada con el valor esperado en el fichero de salida.
            with open('FicheroSalida.csv') as infile2:
                for line2 in zip(infile2):
                    if line2.split(";")[11] != line1.split(";")[11]:
                        outfile.write( time.strftime("%H:%M:%S") + "  Valor Byte Semaforo (no hay coincidencia).\n")
                        outfile.write( time.strftime("%H:%M:%S") + "  --> Valor Esperado: " + line1.split(";")[11])
                        outfile.write( time.strftime("%H:%M:%S") + "  --> Valor Obtenido: " + line2.split(";")[11])
                        outfile.write( "--------------------------------------------\n")
                    else:
                        #Mensaje para indicar que las pruebas no tiene errores.
                        outfile.write("No hay errores en las pruebas!!!\n")

#Cerramos ficheros          
infile1.close()
infile2.close()
outfile.close()

I do not know how to solve it. The only thing I intend is to open a second file and check if a string is equal to the second string.

    
asked by AlberM 23.11.2016 в 11:32
source

2 answers

1

I think your confusion may come from my answer in other of your questions. zip returns an iterator containing tuples of pairs formed by taking elements of two iterables in order, would be clearer with an example using two lists:

lista1 = ['Hola', 'Adios', 'Saludos']
lista2 = ['Pedro', 'Juan', 'Maria']
print(*zip(lista1, lista2))

Return us:

('Hola', 'Pedro') ('Adios', 'Juan') ('Saludos', 'Maria')

I used it to get pairs of lines from two different files:

with open('a.csv') as infile1, open('b.csv') as infile2:
    for line1, line2 in zip(infile1, infile2):

In your case, as you only open a file you must remove the function zip

On the other hand it is not necessary to close your files if you use with , that is one of the advantages of using it with files, when the execution is finished the file closes automatically without having to call .close() :

The code should look like this:

import time

with open('FicheroEntrada.csv') as infile1, open('Diferencias.txt', 'w') as outfile:
    for line1 in infile1:
        #Busco en el fichero la línea que me interesa.
        if line1.split(";")[4] == "Valor Byte Semaforo":
            #Después de 5 segundos voy a comprobar como esta el valor en el fichero de salida.
            time.sleep(2)
            #Comparo el valor del fichero de entrada con el valor esperado en el fichero de salida.
            with open('FicheroSalida.csv') as infile2:
                for line2 in infile2:
                    if line2.split(";")[11] != line1.split(";")[11]:
                        outfile.write( time.strftime("%H:%M:%S") + "  Valor Byte Semaforo (no hay coincidencia).\n")
                        outfile.write( time.strftime("%H:%M:%S") + "  --> Valor Esperado: " + line1.split(";")[11])
                        outfile.write( time.strftime("%H:%M:%S") + "  --> Valor Obtenido: " + line2.split(";")[11])
                        outfile.write( "--------------------------------------------\n")
                    else:
                        #Mensaje para indicar que las pruebas no tiene errores.
                        outfile.write("No hay errores en las pruebas!!!\n")

Keep in mind that what you are doing is comparing each line of infile1 with all lines of infile2 . That is to say for the 1st line of infile1 you compare with the 1st, 2nd, 3rd, 4th, etc. of infile2 and so on all. If what you want is to compare the 1st line of infile1 only with the line that belongs to infile2 , the 2nd line with your partner in infile2 , etc then you would have to modify the code.

    
answered by 23.11.2016 / 13:42
source
0

The problem comes with the use of zip on line for line2 in zip(infile2): . The zip returns a tuple and that's why line2 is a tuple and it does not have method split since it is not of type string .

    
answered by 23.11.2016 в 13:02