Interesting! One way I was able to reproduce that is with an extra line at the end of the input file. Here's a successful run:
-> cat -E test.txt
1 0 100$
2 11 150$
3 0 189$
4 0 195$
5 21 245$
-> python test.py
0
11 non-zero!
0
0
21 non-zero!
Non-zero total: 2
Here's a run with an extra line at the end of the csv file:
-> echo >> test.txt
-> cat -E test.txt
1 0 100$
2 11 150$
3 0 189$
4 0 195$
5 21 245$
$
-> python test.py
0
11 non-zero!
0
0
21 non-zero!
Traceback (most recent call last):
File "test.py", line 9, in ?
print row[1],
IndexError: list index out of range
So you might want to look at a way to check the input and trim out any blank lines, if there are any. It could also be something else entirely!...
Here's a simple example that checks to see if the list is empty before trying to process it:
#!/usr/bin/python
import csv
csvInput = csv.reader(open('test.txt', 'rb'), delimiter=' ')
count = 0
for row in csvInput:
if row:
print row[1],
if row[1] != "0":
print "non-zero!"
count += 1
else:
print
print "Non-zero total: %s" % count
Again, I'm terrible at python, but I hope this helps! :)
-G