使用python的csv.writer生成标题行

时间:2014-06-15 15:08:30

标签: python csv

我有一些生成.csv文件的python代码,但是我不知道如何添加列名或标题行。这是我的代码:

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)

我尝试过添加类似的内容:

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

但这显然不起作用

有人有什么想法吗?

1 个答案:

答案 0 :(得分:2)

在循环之前写入标题:

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)