In your code, this line
for col in file_reader:
is actually iterating through the lines or rows of the file, not the columns
and for each col
, you later advance the reader 250 lines in this code
for j in range(0,250):
spyy = float(col[1]) # here you're grabbing the same second item 250 times
max1 = max(max1,spyy) # setting the new max to the same value 250 times
min1 = min(min1,spyy) # setting the new min to the same value 250 times
file_reader.next() # now you advance, but col is the same so ...
# it's like you're skipping 250 lines
this means that each row stored in col
is actually 250 lines after the previous row stored in col
. It's like your skipping through the file in steps of 250.
I rewrote it, based on what you said you wanted to do. See if this makes more sense:
f1= open('PUT/PUT_SELLING.csv')
file_reader = csv.reader(f1)
spyy_values = []
mins = []
maxes = []
# just saying 'for x in file_reader' is all you need to iterate through the rows
# you don't need to use file_reader.next()
# here I'm also using the enumerate() function
# which automatically returns an index for each row
for row_index, row in enumerate(file_reader):
# get the value
spyy_values.append( float(row[1]) )
if row_index >= 249:
# get the min of the last 250 values,
# including this line
this_min = min(spyy_values[-250:])
mins.append(this_min)
# get the max of the last 250 values,
# including this line
this_max = max(spyy_values[-250:])
maxes.append(this_max)
print "total max:", max(maxes)
print "total min:", min(mins)
print "you have %s max values" % len(maxes)
print "you have %s min values" % len(mins)
print "here are the maxes", maxes
print "here are the mins", mins
Keep in mind that csv.reader is an iterator, so the for loop will automatically advance through each line. Check out the example in the documentation.