0

I have a query which returns a tuple and I'm trying to convert the first value from that tuple to a int but it always gives me this error: TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'

I don't understand why it's doing this because the returned values looks like this: [(25,)]

Here is the code:

cursor.execute("SELECT temp FROM temperatures WHERE datetimetest = ?", [datenow])
    currentTemp = cursor.fetchall()
    print(currentTemp) # this gives me [(25,)]
    convertedTemp = int(currentTemp[0])
1
  • 4
    [(25,)] is a list containing a tuple (1 length tuple) OF an int. do not try to convert, just access it using var[0][0] Commented Dec 13, 2018 at 19:42

2 Answers 2

6

because [(25,)] is actually an int inside a tuple inside a list - so the correct call to get the int would be currentTemp[0][0]

Sign up to request clarification or add additional context in comments.

1 Comment

Oh now i understand! I'm kind of new to python so i didn't knew that:( Thanks for helping me!
0

You can convert list of tuples to list of integers

query = "some query that gets list of integers"
cursor.execute(query)
values = cursor.fetchall()

# values now looks like : [(1,), (2,), (3,)] 

i = 0
for v in values:
    values[i] = v[0]
    i += 1

# values now looks like : [1, 2, 3] 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.