Search code examples
pythonstringfunctionexport-to-excel

Function not writing to .xls correctly . Python 2.7.1


When I check my Excel file it only writes one line of data which is the last input.

def Table(fn,fe):

       for fseq in fn:
              count=0
              for k in fseq:
                     if k=='G' or k=='C':
                            count=count+1
              percentage=(float(count)/len(fseq))*100
              fin=open(fe,'w')
              table=str(fseq)+'\t'+str(count)+'\t'+str(percentage)+'\n'
              fin.write(table)
       return fe



tablist=["AAUG","GCGA","AGCG","TCGA"]
fout=Table(tablist, 'abc.xls')

It is supposed to output this:

   AAUG    1   25.0
   GCGA    3   75.0
   AGCG    3   75.0
   TCGA    2   50.0

Instead, my output looks like this:

   TCGA    2   50.0

and return the name of file being written.

Why is it only writing data for the last element of the string?


Solution

  • Figured it out. Created an empty String and then added each value of the table to it was created during the iteration in the for loop. Wrote the string the .xls file