如何将列表值转换为int [英] how to convert a list value into int
问题描述
我有一个pyodbc行的值(9,)
我试图读取一个简单的整数变量的值,我发现,这么难这样做。 (我是新的python所以我可能错过了一些哑)
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 something dumb)
做一个简单的 int(a_row [0])
get -
doing a simple int(a_row[0])
get -
TypeError: int() argument must be a string or a number, not 'pyodbc.Row'
我试图先将该行转换为列表,并且它的结果相同
i try to cast the row into a list first, and its the same result
我不得不强制它成一个整数值工作
i had to force it into a integer value to work
row_id=(2, )
temp_num=0
temp_num = [number for number in row_id[0]]
temp_num=int(temp_num[0])
我只是发现它真的很奇怪,我必须做一些这么愚蠢的事,所以它必须是我的错,
i just find it really bizarre that i have to do something so silly, so it must be my fault, can anyone enlighten me, where i got it wrong?
推荐答案
您的解决方案并没有将它强制为整数;它在 row_id [0]
中创建一个元素数组( Row
一。所以你最初可以只做 a_row [0] [0]
。
Your solution isn't forcing it into an integer; it is creating an array of the elements (the Row
s) in row_id[0]
, and then extracting the first one. So you could have just done a_row[0][0]
initially.
这篇关于如何将列表值转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!