python - 将单列 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 语句,我相信有更简单的方法可以做到这一点,但我真的无法得到它,谢谢。
解决方案
如果我正确理解了您的需求,这就是您需要做的
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
推荐阅读
- javascript - 在关闭浏览器窗口时通过 CGI 运行 bash 脚本
- javascript - 如何通过我的应用程序代码(即 NodeJS)将 cronjob 条目动态添加到基于 ubuntu 的容器中?
- python - 缺少一个参数时如何引发自己的错误?
- php - 只计算多级菜单的前两级
- load - 我无法使用函数 fromJSON 在 Rappid/jointJS 中加载自定义形状
- r - 按顺序匹配向量
- cmake - cmake,如何将项目添加到解决方案中?
- python - python lambda函数语法错误
- php - 如何显示正确的记录
- python - Numpy:将标签转换为索引