首页 > 解决方案 > 将单列 pyodbc 行转换为纯整数值

问题描述

我正在将数据传递给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 行,我将它的条目转换为字符串,这是一个主要带有空格和括号的数字,(像这样:(964005,))只是我希望它被转换为整数所以它可以传递给 sql 语句,我相信有更简单的方法可以做到这一点,但我真的无法得到它,谢谢。

标签: pythonpyodbc

解决方案


如果我正确理解了您的需求,这就是您需要做的

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

推荐阅读