gpt4 book ai didi

python - 以函数中的 TypeError 开头

转载 作者:IT老高 更新时间:2023-10-28 21:45:26 28 4
gpt4 key购买 nike

代码如下:

    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('>')):
header = line[1:].strip()
else:
seq = fp.read().replace('\n','')
seq = seq.replace('\r','') # for windows
break
fp.close()
return (header, seq)

FASTAsequence = readFasta("MusChr01.fa")

我得到的错误是:

TypeError: startswith first arg must be bytes or a tuple of bytes, not str

但是根据文档,startswith 的第一个参数应该是一个字符串......那是怎么回事?

我假设我至少使用 Python 3,因为我使用的是最新版本的 LiClipse。

最佳答案

这是因为您正在以字节模式打开文件,所以您正在调用 bytes.startswith()而不是 str.startswith() .

你需要做line.startswith(b'>') ,这将使 '>'一个 bytes literal .

关于python - 以函数中的 TypeError 开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19827615/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com