\r\nwill be included
Could be just \n too, depending on the OS. You're right, the lines need to be strip-ped
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?
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(',').
\r\nwill be included
Could be just \n too, depending on the OS. You're right, the lines need to be strip-ped
I did use strip and rstrip but same error was there
Did you check if you'd used len as a variable anywhere?
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
I've moved the relevant comment to an answer. Please accept it to give the thread closure.

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.
Log in to answer this question.
I'd recommend avoiding keywords as variable names. For example, your code has
length = columns[3], which might override any function that's also calledlength(). Maybe usexyz_length =I did change this variable earlier it was pos but i was getting same error
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
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 .