AttributeError: 'tuple' object has no attribute 'readlines' [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm new to python environments and I'm currently working on a ml project. While reading a CSV file using readlines function I'm getting "tuple has no attribute readlines". Please someone help me.....
my code is
data_file=(r"C:\Users\Sury teja\temp\mnist_train.csv","r")
print(data_file.readlines())
error is
AttributeError: 'tuple' object has no attribute 'readlines'

You missed open:
data_file = open(r"C:\Users\Sury teja\temp\mnist_train.csv", "r")
print(data_file.readlines())
In the code you wrote, data_file is just a tuple, which looks like this:
('C:\\Users\\Sury teja\\temp\\mnist_train.csv', 'r')

The error here is caused by the parenthesis around two arguments:
(r"C:\Users\Sury teja\temp\mnist_train.csv","r")
This is caused by the fact that parenthesis are serving more than one purpose in Python: they are used for calling - for example - functions, like in this example, but also for initializing immutable data structures called tuples.
By omitting the function name (open) you have initialized a tuple instead of calling the open function. And tuple, like your error suggests, has no attribute readlines.

Related

I keep getting errors with an "if var.startswith" statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I'm trying to make it so that I can input someone into console and have it set a variable to it, and every time with the if statement it gives the error
File "main.py", line 58, in <module>
if meInput.startswith("%send"):
AttributeError: 'builtin_function_or_method' object has no attribute 'startswith'
Here's the code:
if input.startswith("%send"):
myinput = input.split(" ", 2)[2]
channel = client.get_channel(12324234183172)
I've tried putting it into a variable such as variable = input then changing the if statement to match the variable, but it does the same thing.
Read the error message carefully! It is telling you that input is not a string, but a function — a function that would return a string if you called it, but you didn’t. Try this instead:
if input().startswith("%send"):
Note the parentheses. That is how you call a function in Python, and in most other languages.

Python3 AttributeError: module 're' has no attribute 'split' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am running the code below on my ST3/Atom IDE, it raises the exception that the module 're' has no attribute 'split'. But, when I running the code in the cmd Python script it works well. Can anyone interpret this confusing trouble and give me some advice to make this module works on my IDE? Thanks in advance.
The simple code I tested:
import re
re.split(r'[;,\s]\s', 'hello;,world')
This should work, unless you made the mistake of naming another file in the directory re.py in which case it will look through the re.py file (you created) for split instead of looking through the actual re module when you import re.
instead of the re module you can use the inbuilt features for string variables like:
string = "Some string to be splitted"
splitted_string=string.split(" ") # this will split the string from spaces, change the whitespaces to the characters like comma, colon, period, etc.
mylist = splitted_string
output:
['Some','string','to','be','splitted']
I hoped my answer helped you...

Creating a (2,2,3) Numpy array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm simply trying to create a (2,2,3) Numpy array, and have done the following:
a = np.array[[[1,2,3],
[4,5,6]],
[[6,7,8],
[9,10,11]]]
I however get the following error:
File "xyz.py", line 6, in <module>
[9,10,11]]]
TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'
How can I solve this issue?
Thanks.
You forgot parens. You want
a = numpy.array([[[1,2,3],
[4,5,6]],
[[6,7,8],
[9,10,11]]])
You got the "no attribute '__getitem__'" error because putting square brackets after a symbol means "call this object's __getitem__() method with the stuff inside the brackets", this is what's happening when you do a simple dictionary lookup:
>>> a = {1: 2}
>>> a[1]
2
>>> a.__getitem__(1)
2

AttributeError: 'str' object has no attribute '__name__' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
What is causing this error? Isn't it possible commenting out lines like this inside code?
for i in (Class_1, """Class_2, Class_3"""):
name = i.__name__
Class_1, Class_2 and Class_3 are classes declared before the upper code.
Error output:
> Traceback (most recent call last):
File "", line 2, in <module>
name = i.__name__
AttributeError: 'str' object has no attribute '__name__'
Process finished with exit code 1
Error message line edited to fit the example code
Remove the triple-quoted string """Class_2, Class_3""" to avoid iterating over it which is what you're doing in this case so it looks like for i in (Class_1,) (parenthesis are optional).
It seems you want to comment out those unnecessary sides, but please note that those triple-quotes strings technically aren't comments, so they can still affect the script in some areas you didn't intend.
What do you mean by
for i in (Class_1, """Class_2, Class_3"""):
When you iterate over this tuple, the second element is a string, thus causing the error.

TypeError: 'function' object is not subscriptable in sorting algorithm [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am creating a program in Python which is designed to organise a list, I have been using an insertion sort method, however when I execute the program, after I have input the list top organise, I am returned with the error "TypeError: 'function' object is not subscriptable"
My code is below:
def listsort(x):
for i in range(1,len(list)):
value = list[index]
i = index - 1
while i>=0:
if value < list[i]:
list[i+1] = list[i]
list[i] = value
i = i - 1
else:
break
Please help me understand where I have gone wrong here people, it's frying my brain....
list() is a python function.
You can't index functions.
Considering using a different variable name. (like x since that seems to be the list you are trying to sort)
It's worth noting that sort() and sorted() are also functions that you may use, but learning sorting methods is good practice.

Categories