将列表写入CSV

时间:2014-09-22 13:56:58

标签: python python-2.7

此代码

for record in SeqIO.parse(open(file, 'rU'), 'fasta', generic_protein):
    record_id = re.sub(r'\d+_(\d+_\d\#\d+)_\d+', r'\1', record.id)

    if ( not reference_sequence ):
      reference_sequence = record.seq
      reference_name     = record_id
      #continue
    print ",".join([reference_name, record_id, compare_seqs(reference_sequence, record.seq)])

给出看起来像

的终端输出
7065_8#1,8987_2#53,
7065_8#1,8987_2#58,
7065_8#1,8987_2#61,
7065_8#1,8987_2#62,E-G [246]
7065_8#1,8987_2#65,N-K [71],Y-D [223]

我想逐行写一行到CSV,有什么建议吗?

3 个答案:

答案 0 :(得分:1)

您可以使用writerow来保存输出,如下所示:

for record in SeqIO.parse(open(file, 'rU'), 'fasta', generic_protein):
    record_id = re.sub(r'\d+_(\d+_\d\#\d+)_\d+', r'\1', record.id)

if ( not reference_sequence ):
  reference_sequence = record.seq
  reference_name     = record_id
  #continue
line= ",".join([reference_name, record_id, compare_seqs(reference_sequence, record.seq)])
with open(csvfile, "w") as output:
    writer = csv.writer(output, lineterminator='\n')
    writer.writerow([line]) 

答案 1 :(得分:1)

将所有记录打包在嵌套列表中(即,而不是print ','.join(...),而不是records.append([...])),然后您可以使用writerows(records)将它们写入文件。不需要像'.'.join()那样做csv为你做的事情。

为了完整起见:

records = []
for record in SeqIO.parse(open(file, 'rU'), 'fasta', generic_protein):
    record_id = re.sub(r'\d+_(\d+_\d\#\d+)_\d+', r'\1', record.id)

    if ( not reference_sequence ):
      reference_sequence = record.seq
      reference_name     = record_id
      #continue
    records.append([reference_name, record_id, compare_seqs(reference_sequence, record.seq)])

with csv.writer(open('file.csv', 'w')) as fp:
    fp.writerows(records)  # note that it's not writerow but writerows which allows you to write muptiple rows

答案 2 :(得分:1)

您还可以将逗号分隔的字符串(以及quotechar)直接写入文件:

f = open("output.csv","w")
for record in SeqIO.parse(open(file, 'rU'), 'fasta', generic_protein):
  record_id = re.sub(r'\d+_(\d+_\d\#\d+)_\d+', r'\1', record.id)

  if ( not reference_sequence ):
    reference_sequence = record.seq
    reference_name     = record_id
    #continue
  csvrow =  '","'.join([reference_name, record_id, compare_seqs(reference_sequence, record.seq)])
  csvrow = '"'+csvrow+'"'
  print >>f, csvrow
f.close()

使用此方法,您可以打开文件并检查数据是否正在写入,即使脚本正在运行。