0

Currently, I am attempting to retrieve numeric data from different CSV files. I then place that data in lists in Python. However, I'm struggling to get Python to determine if there is a value in each separate list of data that is greater than a certain number. I need to be able to search each list separately for some value at which point Python should return some text of my choice. I'm not sure what I'm missing, but Python doesn't seem to be handling my syntax as I was hoping.

    import csv
    stocks = ['JPM','PG','KO','GOOG']
    for stock in stocks:
        Data = open("%sMin.csv" % (stock), 'r')
        Ticker = []
        for row in Data:
            Ticker.append(row.strip().split(','))
        if Ticker > 735:
            print "%s Minimum" % (stock)

I modified the code the other stock became Ticker to remove the confusion.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user1526586
  • 93
  • 1
  • 3
  • 10
  • You need to tell us what your file format looks like. Which column of each row is your "stock value" ? – jdi Jul 16 '12 at 04:58

2 Answers2

1

There are three things wrong, preventing you from getting the result.

  1. stock is a list of values, so you aren't able to directly do a numeric comparison to the list itself.
  2. You are appending lists to the list. So even if you were operating on each value you will be comparing a list to an int. Either figure out which element to append, or add them all appropriately.
  3. Find the max value in that list to compare to you int.

First, append floats to your list:

float(stock_value)

Second, figure out which index of your split row is the numeric value. If they are all numeric values, then add them all to the list by extending:

# only convert items to float that are not empty strings
stock.extend(float(val) for val in row.strip().split(',') if val.strip())

If its say, the first column, add just that one:

stock.append(float(row.strip().split(',')[0]))

Third, if you simply want to know if a value greater than 735 is in your list, you can simply ask it for the max value of the list:

if max(stock) > 735:
    print "%s Minimum" % (stock)
jdi
  • 90,542
  • 19
  • 167
  • 203
  • That still doesn't seem to work. Python returns an error in relation to the float. "float() argument must be a string or a number. – user1526586 Jul 16 '12 at 04:52
  • @user1526586: Then your row isn't all numbers. I dont see an example of your row format. Which one is a number? – jdi Jul 16 '12 at 04:57
  • I think I created confusion by making an elementary mistake. I modified the code above to make it more clear. – user1526586 Jul 16 '12 at 04:59
  • @user1526586: Yea we only have the info supplied to us. I was just assuming your file contains all numbers. – jdi Jul 16 '12 at 05:00
  • Row is literally a placeholder. It has no intrinsic meaning. – user1526586 Jul 16 '12 at 05:00
  • Then that is not possible. Because if you are splitting the row on comma, and they are all numbers, it should not be hitting a non number unless you have blank sections? – jdi Jul 16 '12 at 05:01
  • @user1526586: I added a little bit to the end of the loop to only add elements that are not empty strings, in case your problem is that you have input like: `1,2,,3,,4` – jdi Jul 16 '12 at 05:03
1

Make sure you convert your stock prices to floats when adding them to the list.

stock.extend(map(float, row.strip().split(',')))

Now get on to your test.

max(stock) > 735

Should do the trick. If the largest number in the list is greater than your target, then you know at least one number is.

A more explicit way is:

any(x > 735 for x in stock)
kindall
  • 178,883
  • 35
  • 278
  • 309