How To Convert A List Value Into Int
i have a pyodbc row with value (9, ) I am trying to read the value into a simple integer variable, and i find that its so hard to do so. (i am new to python so i probably miss some
Solution 1:
Your solution isn't forcing it into an integer; it is creating an array of the elements (the Rows) in row_id[0], and then extracting the first one. So you could have just done a_row[0][0] initially.
Solution 2:
The output of fetchall of a query is a list of pyodbc.Rows:
>>>x=cursor.execute('SELECT x FROM my_tbl limit 5;').fetchall()>>>print(x) # list of elements of type pyodbc.Row
[(3, ), (2, ), (0, ), (5, ), (1, )]
>>>type(x[0])
pyodbc.Row
to get actual integers pulled by the query above do this
>>> [row[0] for row in x]
[3, 2, 0, 5, 1]
Post a Comment for "How To Convert A List Value Into Int"