TypeError: 'str' object is not callable
2
1
Entering edit mode
5.5 years ago
S AR ▴ 80

Im getting an error wile using a condition on a column two of a csv file. The error says:

TypeError                                 Traceback (most recent call last)
<ipython-input-108-e0c74da932d1> in <module>()
      6     gene = columns[2]
      7     length = columns[3]
----> 8     gen_seq_length = len(seq)
      9     gen_seq_length = int(gen_seq_length)
     10     if gen_seq_length >= 90 and gen_seq_length <= 110:

TypeError: 'str' object is not callable

My file contains 4 coulmns: Specie_Name Sequence Gene_name Gene_count

My code is:

data = open ("data.csv")
for column in data:
    columns = column.split(",")
    names = columns[0]
    seq = columns[1]
    gene = columns[2]
    length = columns[3]
    gen_seq_length = len(seq)

    if gen_seq_length >= 90 and gen_seq_length <= 110:
        print (gene)

And i want to Print out the gene names for all genes between 90 and 110 bases long.

Can anyone help?

python • 13k views
ADD COMMENT
1
Entering edit mode

I'd recommend avoiding keywords as variable names. For example, your code has length = columns[3], which might override any function that's also called length(). Maybe use xyz_length =

ADD REPLY
0
Entering edit mode

I did change this variable earlier it was pos but i was getting same error

ADD REPLY
1
Entering edit mode

Hello angelshiza ,

the code you are showing cannot be the (complete) one, where the error message is shown. In the message this line is shown: gen_seq_length = int(gen_seq_length) which is missing in the code.

Could you please provide a full code example and input file with which one can reproduce the problem?

Thanks,

fin swimmer

ADD REPLY
0
Entering edit mode

The error was i assigned variable name with len and length which are already pre-built functions due to which i was getting an error. But now my problem is solve after changing variable names.

Secondly, gen_seq_length = int(gen_seq_length) this line was in my code i commented it .

ADD REPLY
0
Entering edit mode
5.5 years ago
jomo018 ▴ 720

The error and its location indicates that len is a string rather than a function. Perhaps len was redefined? Something like len='abc'. As a side note, notice that \r\n will be included in the last field. Consider column.strip().split(',').

ADD COMMENT
1
Entering edit mode

\r\n will be included

Could be just \n too, depending on the OS. You're right, the lines need to be strip-ped

ADD REPLY
0
Entering edit mode

I did use strip and rstrip but same error was there

ADD REPLY
1
Entering edit mode

Did you check if you'd used len as a variable anywhere?

ADD REPLY
0
Entering edit mode

Thanks. I wrote the code in new window with change name of variables it worked. My mistake was i used len and length as my variable which that disable the function of len when i called it to get the length of my column. Thank you

ADD REPLY
2
Entering edit mode

I've moved the relevant comment to an answer. Please accept it to give the thread closure.

Upvote|Bookmark|Accept

ADD REPLY
0
Entering edit mode
4.0 years ago
warrenfelsh ▴ 10

TypeError: 'str' object is not callable usually means you are using your () notation on a string, and Python tries to use that str object as a function. e.g. "hello world"(), or "hello"("world"). Or, if you have variable str and trying to call str() function. Finally: Do not declare python variable name same with python built-in function name, keyword etc.

ADD COMMENT

Login before adding your answer.

Traffic: 1913 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6