0

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

3
  • You're not looping over all the rows as you're only calling the nextmethod on each csv-reader instance once. You're missing a looping construct. Other than that, it's not clear at all whether the lines should correspond to the same lines (i.e. line numbers) in the other file. Can perhaps the 3rd linein the first file match the 2nd line in the last file? Please extend your question by giving a clearer description of the desired output. Commented May 12, 2016 at 21:46
  • How are your files actually delimited? Also if row1[8] != row2[8]) or row1[9] != row2[9]:print(row1[0] + ':' + row1[8] + '!=' + row2[8]) will replace your current logic Commented May 12, 2016 at 22:07
  • I was wrong. The delimeter is ' ' Commented May 12, 2016 at 22:12

1 Answer 1

1
  • 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]
Sign up to request clarification or add additional context in comments.

5 Comments

It returns number of strings, but after some string it print IndexError: list index out of range
And I compare JT and MC. Can I output 2: JT, I mean that in 2 string JT from file1 != JT from file2 print it
the IndexError is probably because some rows contain fewer than 10 columns, you can add a check to skip these rows. I don't understand what you mean by output 2: JT though
i have in this file last string Total_jt:8, total_jt_filt:8, dtreal:76960, Box avg:101.963 101.963 72.8307, how can I compare columns to this file? When it comes to this line the process is finish?
sounds like you need to run a different comparison for the last line. in this case, you can run the comparison at the end of your script. the last line of each file should be conveniently available in row1 and row2 after the for loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.