[Tutor] Index out of range

Kent Johnson kent_johnson at skillsoft.com
Fri Oct 15 01:51:32 CEST 2004


Kumar,

IndexError means a list subscript is out of range. My guess is that one of 
the rows is missing a data item or is badly-formed in some other way, so 
when you split it you get the wrong number of fields.

You could rewrite your program to check the length of 'col'. If the length 
is not 71 then print the line number and the line itself. Or you could 
catch the IndexError and print i and list[i] in the exception handler.

BTW you might like to use join() to make your insert statement - you could 
write it like this:
"insert into table "+tname+" values('" + "', '".join(col) + "')"

Kent

At 04:16 PM 10/14/2004 -0700, kumar s wrote:
>Dear Group,
>
>   My brother helped me to write a program for writing
>a file contents into SQL statements.
>
>I have a file with 71 columns in tab delimited text.
>
>table-name
>A   B   D   E   F   G     A71
>1   2   3   4   5   6... 71
>2   3   4   5   6   3    45
>5   6   7   3   7   7    34
>.
>.
>.
>number of rows - 10,000.
>
>I wanted to write a python script that writes these
>values like this:
>
>INSERT INTO table-name
>values('1','2','3','4','5','6',.....'71');
>
>
>I have the following script
>from string import *;
>f = open("sql.txt","r");
>text = f.read();
>f.close
>f = open("insert.txt", "w");
>list = split(text,"\n");
>tname = list[0];
>for i in range(len(list)):
>     if(i!=0):
>         col = split(list[i], "\t");
>         f.write("insert into table "+tname+" values(
>"+col[1]+" "+col[2]+" "+col[3]+"
>"+col[4]+""+col[5]+""+col[6]+""+col[7]+""+col[8]+""+col[9]+""+col[10]+""+col[11]+""+col[12]+""+col[13]+""+col[14]+""+col[15]+""+col[16]+""+col[17]+""+col[18]+""+col[19]+""+col[20]+""+col[21]+""+col[22]+""+col[23]+""+col[24]+""+col[25]+""+col[26]+""+col[27]+""+col[28]+""+col[29]+""+col[30]+""+col[31]+""+col[32]+""+col[33]+""+col[34]+"
>
>"+col[35]+""+col[36]+""+col[37]+""+col[38]+""+col[39]+"
>"+col[40]+" "+col[41]+"
>"+col[42]+""+col[43]+""+col[44]+""+col[45]+""+col[46]+""+col[47]+""+col[48]+""+col[49]+""+col[50]+""+col[51]+""+col[52]+""+col[53]+""+col[54]+""+col[55]+""+col[56]+""+col[57]+""+col[58]+""+col[59]+""+col[60]+""+col[61]+""+col[62]+""+col[63]+""+col[64]+""+col[65]+""+col[66]+""+col[67]+""+col[68]+""+col[69]+""+col[70]+")"+"
>"+col[71]+"\n");
>
>
>When I execute this:
>Traceback (most recent call last):
>   File "insert.py", line 11, in ?
>     f.write("insert into table "+tname+" values(
>"+col[1]+" "+col[2]+" "+col[3]+"
>"+col[4]+""+col[5]+""+col[6]+""+col[7]+""+col[8]+""+col[9]+""+col[10]+""+col[11]+""+col[12]+""+col[13]+""+col[14]+""+col[15]+""+col[16]+""+col[17]+""+col[18]+""+col[19]+""+col[20]+""+col[21]+""+col[22]+""+col[23]+""+col[24]+""+col[25]+""+col[26]+""+col[27]+""+col[28]+""+col[29]+""+col[30]+""+col[31]+""+col[32]+""+col[33]+""+col[34]+"
>
>"+col[35]+""+col[36]+""+col[37]+""+col[38]+""+col[39]+"
>"+col[40]+" "+col[41]+"
>"+col[42]+""+col[43]+""+col[44]+""+col[45]+""+col[46]+""+col[47]+""+col[48]+""+col[49]+""+col[50]+""+col[51]+""+col[52]+""+col[53]+""+col[54]+""+col[55]+""+col[56]+""+col[57]+""+col[58]+""+col[59]+""+col[60]+""+col[61]+""+col[62]+""+col[63]+""+col[64]+""+col[65]+""+col[66]+""+col[67]+""+col[68]+""+col[69]+""+col[70]+")"+"
>"+col[71]+"\n");
>IndexError: list index out of range
>
>
>I get index out of range.  I checked the number of
>columns matched the col[] values here. Even then there
>is some problem. Is there some easy method to get this
>done.
>
>Please help.
>
>Thank you.
>
>Kumar.
>
>
>
>__________________________________________________
>Do You Yahoo!?
>Tired of spam?  Yahoo! Mail has the best spam protection around
>http://mail.yahoo.com
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list