55

コードは次のとおりです。

    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ドキュメントによると、最初の引数は文字列であるはずです...では、何が起こっているのでしょうか?

LiClipse の最新バージョンを使用しているため、少なくとも Python 3 を使用していると想定しています。

4

3 に答える 3

69

bytes.startswith()これは、ファイルをバイト モードで開いているためですstr.startswith()

バイトをリテラルline.startswith(b'>')にする必要があり'>'ます。

于 2013-11-07T03:45:07.767 に答える