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

发布于 2024-08-11 01:05:57 字数 539 浏览 3 评论 0原文

我打开了一个包含爆炸结果的文件,并将结果以 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 呢?

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

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?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

烟花肆意 2024-08-18 01:05:57

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

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

您可以打印到文件对象:

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

或者您可以写入它:

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

然后您可以关闭它:

f.close()

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

像这样的东西

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

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

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

多情出卖 2024-08-18 01:05:57

您可以使用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))

或使用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()

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

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

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()
菩提树下叶撕阳。 2024-08-18 01:05:57

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

python your_program.py >output_file.txt

或者,在 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()

There are two general approaches. Outside of python:

python your_program.py >output_file.txt

Or, inside of 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()
Saygoodbye 2024-08-18 01:05:57

由于某种原因,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")

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

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")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文