Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

startswith TypeError in function

Here is the code:

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

The error I'm getting is:

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

But the first argument to startswith is supposed to be a string according to the docs... so what is going on?

I'm assuming I'm using at least Python 3 since I'm using the latest version of LiClipse.

like image 910
user2287873 Avatar asked Nov 07 '13 03:11

user2287873


People also ask

What does startsWith mean in Python?

The startswith() method returns True if the string starts with the specified value, otherwise False.

What does startsWith function do?

STARTSWITH is a string manipulation function that manipulates all string data types (BIT, BLOB, and CHARACTER), and returns a Boolean value to indicate whether one string begins with another.

What is the purpose of startsWith () in JavaScript?

The startsWith() method determines whether a string begins with the characters of a specified string, returning true or false as appropriate.

Is startsWith () case sensitive?

startsWith method is case sensitive.


1 Answers

It's because you're opening the file in bytes mode, and so you're calling bytes.startswith() and not str.startswith().

You need to do line.startswith(b'>'), which will make '>' a bytes literal.

like image 130
TerryA Avatar answered Sep 23 '22 22:09

TerryA