List index out of range, but list has enough elements
__peter__ at web.de
Tue Nov 9 10:37:58 CET 2010
Costin Gamenț wrote:
> Hi, I am trying to read a string as csv, but I encountered an odd
> problem. Here's my code:
> csvfile = csv.reader(datastr.split('\n'), delimiter=';')
> r = ''
> for i in csvfile:
> for j in i:
> print j
> print i
> the "print j" statement works, but "print i" returns "IndexError:
> list index out of range". Am I missing something?
You'll see that there are empty rows in your data. You cannot detect these
for j in i:
because you get zero iterations, i. e. the
statement is never executed for these rows.
More information about the Python-list