import csv def pN(inp): if len(str(inp).strip())==0: return -99.99 return inp out_file=open("ingest_2mass_dummy","w") create_str ="CREATE TABLE TWOMASS (ID INT,RA2000 DOUBLE,DEC2000 DOUBLE,TWOMASSID CHAR(18),JMAG DOUBLE,EJMAG DOUBLE,HMAG DOUBLE,EHMAG DOUBLE,KMAG DOUBLE,EKMAG DOUBLE,Qflg CHAR(3),Rflg CHAR(3),Bflg CHAR(3),Cflg CHAR(3),Xflg INT, Aflg INT, PRIMARY KEY (ID));\n" out_file.write(create_str) inp_file=csv.reader(open('2mass.data'),delimiter="|",quoting=csv.QUOTE_NONE) i=0 for row in inp_file: if len(row)==0: break i=i+1 ins_str="INSERT INTO TWOMASS VALUES(%i,%s,%s,\'%s\',%s,%s,%s,%s,%s,%s,\'%s\',\'%s\',\'%s\',\'%s\',%s,%s);\n" % (i,row[3],row[4],row[5],pN(row[6]),pN(row[7]),pN(row[8]),pN(row[9]),pN(row[10]),pN(row[11]),row[12],row[13],row[14],row[15],row[16],row[17]) out_file.write(ins_str) out_file.close()