How to convert tuple type to int in python?

I am a beginner Python. I want to convert the result of sqlcommand (type tuple) to type int. How can i do this?

import MySQLdb

db = MySQLdb.connect("localhost","root","password","database")
cursor = db.cursor()
cursor.execute("SELECT timestamp FROM unixdb")
u_data = cursor.fetchall()

>>> print u_data
((1424794931452.0,),)

The type u_datais tupleand I want to get a type from it int.

+6
source share
2 answers

You have a tuple inside a tuple. So, you need the first element of the outer tuple, which is u_data[0]: the innermost tuple. And then you need the first element, which u_data[0][0]. To a float, therefore, to get an integer, you want to wrap it all in int(), which will lead us to:

int(u_data[0][0])
+13
source

. , :

...
u_data, _ = cursor.fetchall()
u_data = [int(_) for _ in udata]
0

All Articles