[Tutor] Error in loop. Please help.

kumar s ps_python at yahoo.com
Sat Nov 27 18:19:33 CET 2004


Dear group, 
 I have the following files:



sample_exp_name
Apple
Boy
Cat
Dog
Elephant


sample_name
A
B
C
D
E


	A	B	C	D	E
g1	12.6	23	67	87	87
g2	34	34	35	28	65
g3	34	397	55	224	45
g4	56	78	56	688	77
g5	8	35	42	2357	56
g6	34	56	56	34	59



Based on these I wanted to write an SQL file:

INSERT INTO sample(sample_name)VALUES('A');
INSERT INTO
samp_exp(sample_id,samp_exp_name)VALUES(currval('sample_sample_id_seq'),'Apple');
INSERT into
vals(sample_id,samp_exp_id,val_name,val_val)VALUES(currval('sample_sample_id_seq'),currval('samp_exp_samp_exp_id_seq'),'A','',A);
INSERT into
vals(sample_id,samp_exp_id,val_name,val_val)VALUES(currval('sample_sample_id_seq'),currval('samp_exp_samp_exp_id_seq'),'A','g1',12.6);
INSERT into
vals(sample_id,samp_exp_id,val_name,val_val)VALUES(currval('sample_sample_id_seq'),currval('samp_exp_samp_exp_id_seq'),'A','g2',34);
INSERT into
vals(sample_id,samp_exp_id,val_name,val_val)VALUES(currval('sample_sample_id_seq'),currval('samp_exp_samp_exp_id_seq'),'A','g3',34);
INSERT into
vals(sample_id,samp_exp_id,val_name,val_val)VALUES(currval('sample_sample_id_seq'),currval('samp_exp_samp_exp_id_seq'),'A','g4',56);
INSERT into
vals(sample_id,samp_exp_id,val_name,val_val)VALUES(currval('sample_sample_id_seq'),currval('samp_exp_samp_exp_id_seq'),'A','g5',8);
INSERT into
vals(sample_id,samp_exp_id,val_name,val_val)VALUES(currval('sample_sample_id_seq'),currval('samp_exp_samp_exp_id_seq'),'A','g6',34);



My Script:

import string
from string import split
f= open('test.txt','r')
#-- Reading the file that contains the matrix
fr = f.read()

f2 = open('sampledb.txt','r')
#--(The above line reads sampledb file that contains
sample_names

f2r = f2.read()
f2lines = f2r.split('\n')
f2lines = f2lines[1:-1]
# -- (The above line filters the first element which
is sample_name)

f3 = open('samp_exp.txt','r')
#--(The above line reads sample_exp_.txt file that
containts sample description such as a for apple).
f3r = f3.read()
f3lines = f3r.split('\n')
f3lines = f3lines[1:-1]
flines = fr.split('\n')
samp = flines[0]
samps = samp.split('\t')
samples = samps[1:]
#--(The above lines stores a list of sample
['a','b','c','d','e']
out = open('test.sql','w')
for m in range(len(samples)):
    out.write("INSERT INTO sample(sample_name)VALUES("
"'"+samples[m]+"'" ");" "\n")
    #--Looping over a sample e.g. a
    for d in range(len(f3lines)):
        #--looping over sample name e.g. apple
        out.write("INSERT INTO
samp_exp(sample_id,samp_exp_name)VALUES(currval('sample_sample_id_seq'),""'"+f3lines[d]+"'"");""\n")
        for i in range(len(flines[1:])):
            #-- looping over the values for
g1,g2,g3,g4 in a column row-wise
		cols = split(flines[i],'\t')
		a = samples.index(samples[m])
	       	out.write("INSERT into
vals(sample_id,samp_exp_id,val_name,val_val)VALUES(currval('sample_sample_id_seq'),currval('samp_exp_samp_exp_id_seq'),""'"+samples[m]+"'"",""'"+cols[0]+"'"","+cols[a+1]+");""\n")



My Problem:

In my previous script versions, I could able to loop
through all columns. 

I did something, I dont remember, unfortunately.  Now
I loop through only one column A.  I am unable to make
the script write the similar sql statements for
column, B, C, D and E. 

I am particular about this sequence:
Write sample_name
write samp_exp_name
write all the values for coulmn A


write sample_name
write sampl_exp_name
write all the values for column B


Can any one, PLEASE help me why I am unable to loop
throug second, third columns. Somehow I made it loop
night before yesterday. I did something when my mind
was sleepy  and from then i am baning my head and i am
unable to get it again.

thanks
Kumar.



		
__________________________________ 
Do you Yahoo!? 
Take Yahoo! Mail with you! Get it on your mobile phone. 
http://mobile.yahoo.com/maildemo 


More information about the Tutor mailing list