[Tutor] Comparing files, Counting Value

Michiyo LaBerge mlaberge at labergedaylight.com
Tue Feb 1 00:27:24 CET 2005

Hello all,

I'm very new to Python and have been trying to write a script to 
compare data from 2 files and getting a total. One of the two files 
contains x, y positions of pixels and a color value(z) of each, so it 
has three columns in the file. The other file has two columns; x1, y1. 
I'd like to get only x, y positions which match to x1, y1 of the second 
file and then look up the z value. If the z value is greater than a 
certain number, it counts 1, otherwise it moves on the next x, y 
position. Finally, I'm able to get total count of the pixels that are 
over the threshold. The script I'm using is below step by step, 
however, I haven't been able to figure out the error massage 
"IndexError: list index out of range" on z value. Any comments would be 


file 1:								file 2:
     299     189       8.543e-02					260	168
     300     189       0.000e+00					270	180
     301     189       0.000e+00					299	189
     302     189       0.000e+00					300	170
       0        188       5.095e-02					301	189
       1        188       5.108e-02					  .	   .
       .		.		.						  .         .
       .		.		.						  .	   .


import sys

i=open("file 1") #value data
o=open("file 2") #look-up file
l=open("result", 'w')#result



while line:
     x1=fields[0, 1] in i    #x,y position in file 1
     z=fields[2] in i           #value data in file 1
     x2=fields[0, 1] in o   #x,y position in file 2

     if x1 == x2:

        if z >= 5.000e-02:
           print>>l, count(1)


More information about the Tutor mailing list