[Tutor] Comparing files, Counting Value
Bill Mill
bill.mill at gmail.com
Tue Feb 1 01:13:27 CET 2005
Michiyo,
When you ask a question to the list, you should be more careful to
highlight your problem so that it doesn't seem like you're asking
people to write a script for you. I don't think that's what you were
doing, but just try to reduce your problem to a minimal example in the
future.
I don't know what your script is doing; array[0, 2] is not legal as
far as I know in python (it is legal on numarray.array types, but not
on regular python lists AFAIK). I also don't know what that "in i"
stuff you're doing means. When I run your code, I get a "TypeError:
list indices must be integers" on the fields[0, 1] line. You should
try to write your code in small bites to make it easier to debug.
That said, I dig doing this sort of text processing, so I wrote my own
script to do it. Perhaps it'll help clear up some ideas for you; if
you have any questions, feel free to ask me about them:
==========begin file testprocess.py=====================
one = open('one')
two = open('two')
out = open('out', 'w')
THRESHOLD = .05
#build dict of {(x,y): z} from 'one'
pts = {}
for line in one:
x, y, z = [float(i) for i in line.split()] #convert strings to floats
pts[(x,y)] = z #insert point into
dictionary
#now check to find each pixel in 'two' in the pts dictionary
bigpixels = 0
for line in two:
x, y = [float(i) for i in line.split()] #convert strings to floats
if (x,y) in pts and pts[(x,y)] > THRESHOLD:
bigpixels += 1
print "%d pixels over %f" % (bigpixels, THRESHOLD)
============end file==============================
Peace
Bill Mill
bill.mill at gmail.com
On Mon, 31 Jan 2005 15:27:24 -0800, Michiyo LaBerge
<mlaberge at labergedaylight.com> wrote:
> 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
> appreciated!
>
> Regards,
> Michiyo
>
> 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 . .
> . . . . .
> . . . . .
>
> #!usr/local/bin/python
>
> import sys
>
> i=open("file 1") #value data
> o=open("file 2") #look-up file
> l=open("result", 'w')#result
>
> results={}
>
> line=i.readline()
> line=o.readline()
>
> while line:
> fields=line.split()
> 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:
> read(z)
>
> if z >= 5.000e-02:
> z=1
> count(n=0)
> print>>l, count(1)
>
> i.close()
> o.close()
> l.close()
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list