2

I have a bit of python code that produces a .csv file, however I don't know how to add column names, or a header row. Here is my code:

handle = open(sys.argv[1])
with open('protparams.csv', 'w') as fp: 
    writer = csv.writer(fp, delimiter=',')
    for record in SeqIO.parse(handle, "fasta"): 
            seq = str(record.seq)
            X = ProtParam.ProteinAnalysis(seq)
            data = [seq,X.get_amino_acids_percent(),X.aromaticity(),X.gravy(),X.isoelectric_point(),X.secondary_structure_fraction(),X.molecular_weight(),X.instability_index()]
            writer.writerow(data)

I have tried adding in something like:

writer = csv.writer(fp, delimiter=',',[seq,aa_percentage,aromaticity,gravy,isoelectric_point,secondary_structure_fraction,molecular_weight,instability_index])

but this obviously doesn't work

anyone have any ideas?

1 Answer 1

2

Write the headers before the loop:

handle = open(sys.argv[1])
with open('protparams.csv', 'w') as fp:
    writer = csv.writer(fp, delimiter=',')
    writer.writerow(['heading1','heading2','heading3'])
    for record in SeqIO.parse(handle, "fasta"):
            seq = str(record.seq)
            X = ProtParam.ProteinAnalysis(seq)
            data = [seq,X.get_amino_acids_percent(),X.aromaticity(),X.gravy(),X.isoelectric_point(),X.secondary_structure_fraction(),X.molecular_weight(),X.instability_index()]
            writer.writerow(data)
Sign up to request clarification or add additional context in comments.

1 Comment

ah so simple, how I didn't see it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.