【问题标题】:Saving output of a for-loop to file将 for 循环的输出保存到文件
【发布时间】:2010-12-13 15:23:17
【问题描述】:

我打开了一个包含爆炸结果的文件,并将结果以 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() 替换循环中的打印语句,但是我们编写的 '>'、alignment.title 将如何?

【问题讨论】:

    标签: python file loops save


    【解决方案1】:

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

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

    【讨论】:

    • 所有打开文件后的操作都应该用try ... finally包裹起来,以确保正确关闭文件
    • 在一个长时间运行的过程中,是的,在一个简单的脚本中没有关系,因为脚本退出时文件将被关闭。
    【解决方案2】:

    类似的东西

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        有两种通用方法。在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()
        

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            猜你喜欢
            • 2021-07-11
            • 1970-01-01
            • 2022-11-28
            • 2020-11-01
            • 2018-05-24
            • 2021-10-27
            • 1970-01-01
            • 2015-10-13
            • 2021-09-13
            相关资源
            最近更新 更多