编码:
def readFasta(filename):
""" Reads a sequence in Fasta format """
fp = open(filename, 'rb')
header = ""
seq = ""
while True:
line = fp.readline()
if (line == ""):
break
if (line.startswith(b'>')):
header = line[1:].strip()
else:
seq = fp.read().replace(b'\n',b'')
seq = seq.replace(b'\r',b'') # for windows
break
fp.close()
return (header, seq)
FASTAsequence = readFasta("MusChr01.fa")
'' 之前的 b 是必要的,因为我处于字节模式。问题是运行时, fp.read.replace 和 seq.replace 删除字符串中的所有内容。我确实知道 read 工作正常,因为
def readFasta(filename):
""" Reads a sequence in Fasta format """
fp = open(filename, 'rb')
header = ""
seq = ""
while True:
line = fp.readline()
if (line == ""):
break
if (line.startswith(b'>')):
header = line[1:].strip()
else:
seq = fp.read()
break
fp.close()
return (header, seq)
FASTAsequence = readFasta("MusChr01.fa")
工作得很好。这里发生了什么?