List index out of range, but list has enough elements

Stefan Behnel stefan_ml at behnel.de
Tue Nov 9 04:33:28 EST 2010


Costin Gamenț, 09.11.2010 10:24:
> 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[0]
>
> the "print j" statement works, but "print i[0]" returns "IndexError:
> list index out of range". Am I missing something?

Are you sure the output you get from the "print j" is from the same loop 
iteration as the "print i[0]"? Try adding "i" to the output.

Stefan




More information about the Python-list mailing list