Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > List index out of range, but list has enough elements

Reply
Thread Tools

List index out of range, but list has enough elements

 
 
Costin Gamenț
Guest
Posts: n/a
 
      11-09-2010
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?
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      11-09-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[0]
>
> the "print j" statement works, but "print i[0]" returns "IndexError:
> list index out of range". Am I missing something?


Change

print i[0]

to

print i

You'll see that there are empty rows in your data. You cannot detect these
rows with

for j in i:
print j

because you get zero iterations, i. e. the

print j

statement is never executed for these rows.

Peter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sorting index-15, index-9, index-110 "the human way"? Tomasz Chmielewski Perl Misc 4 03-04-2008 05:01 PM
L A county says enough is enough richard Computer Support 7 02-26-2008 03:27 AM
Enough is enough... Imhotep Computer Security 16 09-28-2005 03:36 PM
Enough is enough.... ajacobs2 Digital Photography 33 10-05-2003 12:14 PM
Resolution - when is Enough ENOUGH? (a personal view) VT Digital Photography 43 09-12-2003 11:15 AM



Advertisments