0

Similar to the the question here, I need to compare two CSV files and print out the changes. The only difference to this question if I have a number inputs with this name I want to write them all to the Differences.csv file. I'm using the same code as the link above:

with open('old_file.csv','rb') as file1:
   existingLines = [line for line in csv.reader(file1, delimiter=',')]

   with open('new_file.csv','rb') as file2:
       reader2 = csv.reader(file2,delimiter=',')

       with open('Differences.csv', 'wb') as h:
           writer = csv.writer(h)

           for row in reader2:
               if row not in new and row not in existingLines:
                   new.append(row) 

I've an example below of the output I want below:

old_file =                          new_file = 
Fruit Length Width                  Fruit Length Width
Grape  0.3    0.4                   Grape  0.3    0.4
Grape  0.2    0.5                   Grape  0.2    0.5
Apple  0.8    1.0                   Apple  0.8    1.0
Apple  1.0    1.1                   Apple  1.0    1.0
                                    Plum   1.3    1.4
                                    Plum   1.1    1.2

Differences = 
Fruit Length Width 
Apple  0.8    1.0
Apple  1.0    1.0
Plum   1.3    1.4
Plum   1.1    1.2

So as the apples width has changed I want to print both apples again. Many thanks

CDJB
  • 14,043
  • 5
  • 29
  • 55
MRaff16
  • 5
  • 5

1 Answers1

0

It feels natural to do it in two steps:

  • Generate a list of names with changes as described in link
  • Then copy all the lines with names from the list
Kirill Fedyanin
  • 572
  • 5
  • 22
  • Thanks. I was thinking of doing it this way, but I keep getting stuck with having grape also in the output file also. – MRaff16 Feb 06 '20 at 12:39