Python error : 'tuple' object has no attribute 'upper' - Stack Overflow
Python openpyxl module says: AttributeError: 'tuple' object has no attribute 'upper' - Stack Overflow
AttributeError: 'tuple' object has no attribute 'upper'
python - AttributeError: 'tuple' object has no attribute - Stack Overflow
Videos
I am trying to make a discord bot that would turn a message into lowercase. I am encountering an error, as the title suggests, "AttributeError: 'tuple' object has no attribute 'lower'. "
Here is my code if anyone can help.
https://hastebin.com/ibareyilax.py
If this is the wrong subreddit I don't mind taking down my post and posting it elsewhere.
If you print the value of words[i] after your attempted character replacements you will see that it is set to a tuple, e.g.
('word', (',', ''), ('/', ''), ('?', ''), ('!', ''))
So the line that tries to remove unwanted punctuation actually creates a tuple because that's what the comma separated items are, i.e.
words[i].replace(".", ""), (",", ""), ("/", ""), ("?", ""), ("!", "")
is actually a tuple consisting of words[i].replace(".", "") followed by (",", ""), etc.
You might have meant to chain a whole lot of replace operations together, but that would need to look like this:
words[i].replace(".", "").replace(",", "").replace("/", "").replace("?", "").replace("!", "")
But that is pretty ugly, and it's restricted to just a few punctuation symbols. str.translate() is better:
words[i] = words[i].translate(None, '.,/?!')
or, if you want to get rid of all punctuation you can use string.punctuation:
import string
words[i] = words[i].translate(None, string.punctuation)
Or, if you are using Python 3:
import string
words[i] = words[i].({ord(c):None for c in string.punctuation})
There are other problems in your code, but see if you can correct this first issue first.
in this line:
words[i] = words[i].replace(".", ""), (",", ""), ("/", ""), ("?", ""), ("!", "")
you assign tuple into words[i].
i guess you want to replace several character and that you mean to do this:
words[i] = words[i].replace(".", "").replace(",", "").replace("/", "").replace("?", "").replace("!", "")
several values with comma between them are tuple.
1,5,6 is the same as (1,5,6)
so
words[i].replace(".", ""), (",", ""), ("/", ""), ("?", ""), ("!", "")
is the same as
(words[i].replace(".", ""), (",", ""), ("/", ""), ("?", ""), ("!", ""))
in addition, you can't assign into tuple, therefore. the line
wordCount[words[i]] = 1
can throw an exception, you need to change wordCountint to a dict (when you create it):
wordCount = {}
You return four variables s1,s2,s3,s4 and receive them using a single variable obj. This is what is called a tuple, obj is associated with 4 values, the values of s1,s2,s3,s4. So, use index as you use in a list to get the value you want, in order.
Copyobj=list_benefits()
print obj[0] + " is a benefit of functions!"
print obj[1] + " is a benefit of functions!"
print obj[2] + " is a benefit of functions!"
print obj[3] + " is a benefit of functions!"
You're returning a tuple. Index it.
Copyobj=list_benefits()
print obj[0] + " is a benefit of functions!"
print obj[1] + " is a benefit of functions!"
print obj[2] + " is a benefit of functions!"
from kubernetes import client, config
config.load_kube_config()
v1 = client.CoreV1Api()
print("Listing services with their IPs:")
ret = v1.list_service_for_all_namespaces_with_http_info(watch=False)
for i in ret.items:
print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name)) This throws this error: File "filename.py", line 8, in <module> for i in ret.items: AttributeError: 'tuple' object has no attribute 'items'
But when I simply print(ret), it certainly LOOKS like a tuple, which means I should be able to iterate through with tuple.items, no?