Python: Check Various Text Files

1

Is it possible to check that the values obtained in a .csv file called A are the same as in another file .csv B and print the different cases in a file output.txt? I have done this:

import time

# En primer lugar debemos de abrir el fichero que vamos a leer.
# y abrir el fichero donde vamos a guardar la informacion.
infile = open('Intercambio Modbus v11 - RGA.csv', 'r')
outfile = open("salida.txt","w")
# Mostramos por pantalla lo que leemos desde el fichero
print('>>> Lectura del fichero línea a línea')

# Timer de 2 segundos para probar si funciona, podemos meter timer para que espere unos segundos antes de comprobar una salida.
# time.sleep(2)

for line in infile:
    line = line[:-1]
    split_line = line.split(";")
    if split_line[5] == split_line[11]:
        outfile.write("La línea es correcta\n")
        #time.sleep(3)
    else:
        outfile.write("NO COINCIDEN LOS VALORES --> " + line + "\n")

# Cerramos el fichero.
infile.close()
outfile.close()

With this file I check the values that I want to know if they are the same in the same file but ... What happens if instead of split_line[11] of the same file I would like to check another file? How can I do it?

    
asked by AlberM 21.11.2016 в 09:32
source

1 answer

0

If your two files have the same number of lines you can use the pre-built function zip() that returns an iterator with tuples formed by the pairs of two iterables. If one of the files has more lines than the other, only those that have a partner in the other will be compared, that is, the more lines are not taken into account. It would be something like this:

import time

# En primer lugar debemos de abrir el fichero que vamos a leer.
# y abrir el fichero donde vamos a guardar la informacion.
infile1 = open('a.csv', 'r')
infile2 = open('b.csv', 'r')
outfile = open("salida.txt","w")
# Mostramos por pantalla lo que leemos desde el fichero
print('>>> Lectura del fichero línea a línea')

# Timer de 2 segundos para probar si funciona, podemos meter timer para que espere unos segundos antes de comprobar una salida.
# time.sleep(2)

for line1, line2 in zip(infile1, infile2):
    line1 = line1[:-1]
    line2 = line2[:-1]

    if line1.split(";")[5] == line2.split(";")[11]:
        outfile.write("La línea es correcta\n")
        #time.sleep(3)
    else:
        outfile.write("NO COINCIDEN LOS VALORES --> " + line1 + "\n")

# Cerramos el fichero.
infile1.close()
infile2.close()
outfile.close()

You could use with to simplify if you want:

with open('a.csv') as infile1, open('b.csv') as infile2, open('salida.txt', 'w') as outfile:
    for line1, line2 in zip(infile1, infile2):
        line1 = line1[:-1]
        line2 = line2[:-1]

        if line1.split(";")[5] == line2.split(";")[11]:
            outfile.write("La línea es correcta\n")
        else:
            outfile.write("NO COINCIDEN LOS VALORES --> " + line1 + "\n")

If you often work with csv files, I recommend you look at the module csv that belongs to the standard library of Python and facilitates the reading / writing of this type of files.

    
answered by 21.11.2016 / 11:19
source