How to save output from python like tsv

前端 未结 5 1194
没有蜡笔的小新
没有蜡笔的小新 2021-01-02 02:10

I am using biopython package and I would like to save result like tsv file. This output from print to tsv.

for record in SeqIO.parse(\"/home/fil/Desktop/420_         


        
5条回答
  •  鱼传尺愫
    2021-01-02 02:53

    That is fairly simple , instead of printing it you need to write that to a file.

    with open("records.tsv", "w") as record_file:
        for record in SeqIO.parse("/home/fil/Desktop/420_2_03_074.fastq", "fastq"):
            record_file.write("%s %s %s\n" % (record.id,record.seq, record.format("qual")))
    

    And if you want to name the various columns in the file then you can use:

    record_file.write("Record_Id    Record_Seq    Record_Qal\n")
    

    So the complete code may look like:

    with open("records.tsv", "w") as record_file:
        record_file.write("Record_Id    Record_Seq    Record_Qal\n")
        for record in SeqIO.parse("/home/fil/Desktop/420_2_03_074.fastq", "fastq"):
            record_file.write(str(record.id)+"  "+str(record.seq)+"  "+ str(record.format("qual"))+"\n")
    

提交回复
热议问题