Arseniy Krupenin
Arseniy Krupenin

Reputation: 3880

Compare some columns from 2 csv files using python

Input is 2 csv files

EID MolIdx TEStart TEEnd TE TZone TBulkBE TBulkAE MC JT zavg vabs vzavg  xyd.x xyd.y xydist nnbw vabsprev midhb
0 370 36700 36800 110 20 36150 37090 0 0 -8.25705 0.219113 -0.000800014 20.8926 41.4347 5.75852 0 4.13067 0 
1 423 17950 18150 210 180 17400 18430 1 0 -4.26426 0.586578 -0.053 77.22 85.2104 22.0534 0 3.551 0 
2 468 41790 42020 240 50 41360 42380 0 0 7.82681 0.181248 -0.00269566 90.0646 92.7698 5.0841 0 4.19304 0 

and

EID MolIdx TEStart TEEnd TE TZone TBulkBE TBulkAE MC JT zavg vabs vzavg xyd.x xyd.y xydist nnbw vabsprev midhb
0 370 36700 36800 110 20 36150 37090 0 0 -0.846655 0.0218695 2.59898e-05 2.0724 4.1259 0.583259 10 0.412513 0 
1 423 17950 18150 210 180 17400 18780 1 0 -0.453311 0.058732 -0.00526783 7.7403 8.52544 2.19627 0 0.354126 0 
2 468 41790 42020 240 70 41360 42380 0 0 0.743716 0.0181613 -0.000256186 9.08777 9.21395 0.502506 0 0.419265 0 

I need to compare columns MC and JT from file1 with this columns from file2

Desire output:

Number_of_strings

Print strings, where values are different

import csv

old = csv.reader(open('old.csv', 'rb'), delimiter=',')
row1 = old.next()
new = csv.reader(open('new.csv', 'rb'), delimiter=',')
row2 = new.next()

if (row1[8] == row2[8]) and (row1[9] == row2[9]):
    continue
else:
    print row1[0] + ':' + row1[8] + '!=' + row2[8]

But it does't work for me

Upvotes: 0

Views: 82

Answers (1)

Fabricator
Fabricator

Reputation: 12772

  • delimiter is instead of ,
  • you forgot to use a for loop to iterate over the rest of the files:

code:

import csv

old = csv.reader(open('old.csv', 'rb'), delimiter=' ')
row1 = old.next()
new = csv.reader(open('new.csv', 'rb'), delimiter=' ')
row2 = new.next()

for row1, row2 in zip(old, new):
    if (row1[8] == row2[8]) and (row1[9] == row2[9]):
        continue
    else:
        print row1[0] + ':' + row1[8] + '!=' + row2[8]

Upvotes: 1

Related Questions