0

Say I have the following CSV file;

1.41, 123456
1.42, 123456
1.43, 123456

and i want to find the "position"/location of a value in row 0 i.e. "1.41, 1.42, 1.43" in this case, depending on whether the particular row value is greater than or equal to an arbitrary inputted value.

So for example if the inputted value was 1.42, we would return position 0 and 1, or if the inputted value was 1.4, we would return 0, 1 and 2. Likewise, if the value was 1.45 we would not return any positions. Here is what i have:

out = open("test.csv","rU")
dataf=csv.reader(out, delimiter=',')

for row in dataf:
    x = row[0]


    for i in xrange(len(x)):
    if x[i] >=1 :
        print i

only to get,

0
1
2
3
0
1
2
3
0
1
2
3

so then i use

for i in xrange(len(x)):
if x[i] >=2 :
    print i

But i still get the same position values. Can anyone steer me in the right direction?

user3439187
  • 613
  • 1
  • 7
  • 10
  • First off, kindly make sure if the indentation above are an error on the posting side or exactly the same as your code. Second, are you trying -- in effect -- to find the row number based on the value? Say, `1.43` to return `3`? – WGS Apr 09 '14 at 22:49
  • Could you explain what you mean by "position of a value in row 0"? Perhaps say what the expected output in the example above is. – s16h Apr 09 '14 at 22:52
  • Thanks, the indentation is correct, but it would work either way. And yes, i need to return the position "2" if the value was equal to or greater than 1.43. (the position is 2 because we start from 0, as you probably know) – user3439187 Apr 09 '14 at 22:54
  • `if the value was equal to or greater than 1.43.` -- Does this mean that if you search for `1.44` given the above CSV, it should still return the third row? Hm. I may have posted an answer a few seconds too early. – WGS Apr 09 '14 at 22:56
  • Hi s16h, the purpose is to find the location of the value(s), say for example if x[i] == 1.42, we would return the posion "1". – user3439187 Apr 09 '14 at 22:56

1 Answers1

1

From what I can gather, this does what you're asking...

#!/usr/bin/env python

import csv

value = 1.42

out = open("test.csv","rU")
dataf=csv.reader(out, delimiter=',')
matches = [float(row[0]) >= value for row in dataf]
matches.reverse()

for i,m in enumerate(matches):
    if m: 
        print i

matches is a list of boolean values, that shows whether the first column in each row is greater than value. It looks like you want to order from the bottom up, so I reversed the list. The loop prints the index of the (reversed) list if the value in the first column was greater than or equal to value.

value = 1.42
output:

0
1

value = 1.4
output:

0
1
2

value = 1.45
no output.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
  • Nice one Tom, very much does the trick. What a relief. The only edit was that i removed the matches.reverse() - i know i implied that in my question, but i never meant to, as the order of the values is random in practice. Much appreciated. – user3439187 Apr 09 '14 at 23:50
  • @user3439187 no problem. I see that you are relatively new to this site and have a couple of questions that appear to be solved. Don't forget to accept answers and upvote the ones that you find useful. – Tom Fenech Apr 10 '14 at 08:13
  • I have tried but they wont let me until i have answered a few questions and built up a reputation! Sorry mate, i'll come back to it. – user3439187 Apr 11 '14 at 04:19