热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

将for循环的输出保存到文件-Savingoutputofafor-looptofile

Ihaveopenedafilewithblastresultsandprintedoutthehitsinfastaformattothescreen.我打开了

I have opened a file with blast results and printed out the hits in fasta format to the screen.

我打开了一个带有爆破结果的文件,并以fasta格式打印到屏幕上。

The code looks like this:

代码如下所示:

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

This outputs a list of fasta files to the screen. But how can I create a file and save the fasta output to this file?

这会将fasta文件列表输出到屏幕。但是如何创建文件并将fasta输出保存到此文件中?

Update: I guess I would have to replace the print statements within the loop with something.write(), but how will the '>', alignment.title we written?

更新:我想我必须用something.write()替换循环中的print语句,但是我们如何编写'>',alignment.title?

5 个解决方案

#1


7  

First, create a file object:

首先,创建一个文件对象:

f = open("myfile.txt", "w") # Use "a" instead of "w" to append to file

You can print to a file object:

您可以打印到文件对象:

print >> f, '>', alignment.title
print >> f, hsp.sbjct 

Or you can write to it:

或者你可以写信给它:

f.write('> %s\n' % (alignment.title,))
f.write('%s\n' % (hsp.sbjct,))

You can then close it to be nice:

然后你可以把它关闭好看:

f.close()

#2


4  

Something like this

像这样的东西

with open("thefile.txt","w") as f
  for alignment in blast_record.alignments:
    for hsp in alignment.hsps:
      f.write(">%s\n"%alignment.title)
      f.write(hsp.sbjct+"\n")

prefer not to use print >> as that won't work anymore in Python3

不喜欢不使用print >>因为它在Python3中不再起作用

#3


4  

you can use with statement to ensure that file will be closed

您可以使用with语句来确保文件将被关闭

from __future__ import with_statement

with open('/Users/jonbra/Desktop/my_blast.xml', 'w') as outfile:
    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:
            outfile.write('>%s\n%s\n' % (alignment.title, hsp.sbjct))

or use try ... finally

或者最后使用try ...

outfile = open('/Users/jonbra/Desktop/my_blast.xml', 'w')
try:
    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:
            outfile.write('>%s\n%s\n' % (alignment.title, hsp.sbjct))
finally:
    outfile.close()

#4


2  

There are two general approaches. Outside of python:

有两种一般方法。在python之外:

python your_program.py >output_file.txt

Or, inside of Python:

或者,在Python内部:

out = open("output_file.txt", "w")
for alignment in blast_record.alignments:
    for hsp in alignment.hsps:
        print >>out, '>', alignment.title
        print >>out, hsp.sbjct
out.close()

#5


0  

for some reason the code above posted by OP did not work for me.. I modified a bit

由于某种原因,OP发布的上述代码对我不起作用..我修改了一下

from Bio.Blast import NCBIXML
f = open('result.txt','w')
for record in NCBIXML.parse(open("file.xml")) :
    for alignment in record.alignments:
        for hsp in alignment.hsps:
            f.write(">%s\n"%alignment.title)
            f.write(hsp.sbjct+"\n")

推荐阅读
author-avatar
赵lamarta
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有