python 将 for 循环的输出保存到文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1684194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:51:38  来源:igfitidea点击:

Saving output of a for-loop to file

pythonfileloopssave

提问by Jon

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() 替换循环中的打印语句,但是我们写的 '>',alignment.title 将如何?

回答by truppo

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()

回答by John La Rooy

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 中不再适用

回答by Victor Kotseruba

you can use with statementto ensure that file will be closed

您可以使用with statement以确保文件将被关闭

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 ... finally

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()

回答by Greg Hewgill

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()

回答by user3224522

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")