将单列 pyodbc 行转换为普通整数值
Convert a single-column pyodbc row into a plain integer value
我正在将数据传递给python中的sql语句,代码部分是这样的:
foo.execute("select test_dat_id from tbl_dat_test where sample_id =?",(n_value,))
myfoo = foo.fetchall()
zoo = "select result_value from tbl_dat_analyte where test_dat_id ="
for new in myfoo:
new1=str(new)
new2=float(new1)
var = zoo + new2
print(var)
foo.execute(var)
长话短说,myfoo 是 sql 行,我将其条目转换为字符串,这是一个主要带有 space 和括号的数字,(像这样:(964005 , ))
只是我希望将它转换为整数,以便它可以传递给 sql 语句,我相信有更简单的方法可以做到这一点,但我真的做不到,谢谢。
如果我正确理解了您的需求,这就是您需要做的
string_with_number_and_whitespaces = '(100) ' # Also contains brackets
string_with_number_only = string_with_number_and_whitespaces.replace(' ', '').replace('(', '').replace(')', '')
number = int(string_with_number_only)
print(number) # Output 100
一个好方法是使用正则表达式:
import re
foo = "(100) "
foo = re.sub("[^0-9]", "", foo)
foo = int(foo)
这将删除所有非数字字符,并将字符串转换为 int。
.fetchall()
returns pyodbc.Row
个对象的列表。如果您想要行中的第一个(也是唯一一个)元素,只需使用索引 [0]
提取它
>>> myfoo = [(1,),(2,)]
>>> for new in myfoo:
... myint = new[0]
... print(myint * 10)
...
10
20
>>>
非常感谢您提供所有宝贵的答案,我将一一尝试,实际上在我收到您的反馈之前,我已经像这样工作了(我知道这是一个原始的解决方案,但它工作):
foo.execute("select test_dat_id from tbl_dat_test where sample_id =?",(n_value,))
myfoo = foo.fetchall()
myfoo2 = dict(enumerate(item[0:100] for item in myfoo))
v_value = list(myfoo2.values())[0:100]
zoo = "select result_value from tbl_dat_analyte where test_dat_id = "
for new in v_value:
new2=str(new)
new2=new2.replace(" ", "")
new2=new2.replace("(", "")
new2=new2.replace(",)", "")
var = zoo + new2
print(var)
foo100 = cnx.cursor()
foo100.execute(var)
myzoo = foo100.fetchall()
print('zoo is:')
print(myzoo)
c5a = my_sheet.cell(row= 21 , column = 3)
c5a.value = myzoo
我正在将数据传递给python中的sql语句,代码部分是这样的:
foo.execute("select test_dat_id from tbl_dat_test where sample_id =?",(n_value,))
myfoo = foo.fetchall()
zoo = "select result_value from tbl_dat_analyte where test_dat_id ="
for new in myfoo:
new1=str(new)
new2=float(new1)
var = zoo + new2
print(var)
foo.execute(var)
长话短说,myfoo 是 sql 行,我将其条目转换为字符串,这是一个主要带有 space 和括号的数字,(像这样:(964005 , )) 只是我希望将它转换为整数,以便它可以传递给 sql 语句,我相信有更简单的方法可以做到这一点,但我真的做不到,谢谢。
如果我正确理解了您的需求,这就是您需要做的
string_with_number_and_whitespaces = '(100) ' # Also contains brackets
string_with_number_only = string_with_number_and_whitespaces.replace(' ', '').replace('(', '').replace(')', '')
number = int(string_with_number_only)
print(number) # Output 100
一个好方法是使用正则表达式:
import re
foo = "(100) "
foo = re.sub("[^0-9]", "", foo)
foo = int(foo)
这将删除所有非数字字符,并将字符串转换为 int。
.fetchall()
returns pyodbc.Row
个对象的列表。如果您想要行中的第一个(也是唯一一个)元素,只需使用索引 [0]
>>> myfoo = [(1,),(2,)]
>>> for new in myfoo:
... myint = new[0]
... print(myint * 10)
...
10
20
>>>
非常感谢您提供所有宝贵的答案,我将一一尝试,实际上在我收到您的反馈之前,我已经像这样工作了(我知道这是一个原始的解决方案,但它工作):
foo.execute("select test_dat_id from tbl_dat_test where sample_id =?",(n_value,))
myfoo = foo.fetchall()
myfoo2 = dict(enumerate(item[0:100] for item in myfoo))
v_value = list(myfoo2.values())[0:100]
zoo = "select result_value from tbl_dat_analyte where test_dat_id = "
for new in v_value:
new2=str(new)
new2=new2.replace(" ", "")
new2=new2.replace("(", "")
new2=new2.replace(",)", "")
var = zoo + new2
print(var)
foo100 = cnx.cursor()
foo100.execute(var)
myzoo = foo100.fetchall()
print('zoo is:')
print(myzoo)
c5a = my_sheet.cell(row= 21 , column = 3)
c5a.value = myzoo