[英]Converting tuple to int in python
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'
我有一个返回元组的查询,我正在尝试将该元组中的第一个值转换为 int 但它总是给我这个错误:
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,)]
我不明白为什么要这样做,因为返回的值如下所示:
[(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])
因为[(25,)]
实际上是列表中元组内的 int - 所以获取 int 的正确调用将是currentTemp[0][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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.