我打开了一个包含blast结果的文件,并以fasta格式将点击结果打印到屏幕上。
代码如下所示:
result_handle = open("/Users/jonbra/Desktop/my_blast.xml")
from Bio.Blast import NCBIXML
blast_records = NCBIXML.parse(result_handle)
blast_record = blast_records.next()
for alignment in blast_record.alignments:
for hsp in alignment.hsps:
print '>', alignment.title
print hsp.sbjct
这将向屏幕输出一个fasta文件列表。 但是如何创建一个文件并将fasta输出保存到这个文件中呢?
更新:我想我必须用something.write()替换循环中的print语句,但是我们编写的'>;'alignment.title将如何替换?
您可以使用
with statement
来确保文件将被关闭或者使用
try ... finally
首先,创建一个文件对象:
可以打印到文件对象:
或者你可以写信给它:
然后,你可以关闭它以使其更美观:
像这样的东西
不要使用
print >>
,因为这在Python3中不再有效相关问题 更多 >
编程相关推荐