在這裏我做了一個簡單的程序,通過一個文本文件,其中包含一些細菌基因組中的基因,包括編碼這些基因的氨基酸(明確使用更合適?)我非常依賴Biopython中的模塊。無法使用打印語句寫入文件
這在我的Python shell中運行良好,但我無法將它保存到文件中。
這工作:
import Bio
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.Alphabet import IUPAC
from Bio.SeqUtils import GC
from Bio.SeqUtils import ProtParam
for record in SeqIO.parse("RTEST.faa", "fasta"):
identifier=record.id
length=len(record.seq)
print identifier, length
但這並不:
import Bio
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.Alphabet import IUPAC
from Bio.SeqUtils import GC
from Bio.SeqUtils import ProtParam
for record in SeqIO.parse("RTEST.faa", "fasta"):
identifier=record.id
length=len(record.seq)
print identifier, length >> "testing.txt"
也不是這:
import Bio
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.Alphabet import IUPAC
from Bio.SeqUtils import GC
from Bio.SeqUtils import ProtParam
f = open("testingtext.txt", "w")
for record in SeqIO.parse("RTEST.faa", "fasta"):
identifier=record.id
length=len(record.seq)
f.write(identifier, length)
也不是這:
import Bio
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.Alphabet import IUPAC
from Bio.SeqUtils import GC
from Bio.SeqUtils import ProtParam
f = open("testingtext.txt", "w")
for record in SeqIO.parse("RTEST.faa", "fasta"):
identifier=record.id
length=len(record.seq)
f.write("len(record.seq) \n")
你說他們不工作;他們在做什麼? – jonrsharpe
前兩個不會打印到文件,因爲第一個將打印到stdout,而'>>是在python中的一個位移操作。其他兩種方法應該。請注意,示例代碼的最後一行將'len(record.seq)\ n'寫入您的文件,而不是數字的實際長度。你有什麼錯誤嗎? – msvalkon
@msvalkon第三個會崩潰,因爲f.write只接受一個必須是字符串的參數。 嘗試f.write('{0},{1}'.format(identifier,length))。另外,不要忘記關閉文件(或與之一起使用)。 – AMacK