如何移除传入存储过程的price变量中的单引号
问题描述
开发最后环节遇到阻滞,需要移除解析得到后传入存储过程的price变量内的单引号,当前实现代码如下:
Body_content = message.Body Body_content = Body_content[:Body_content.find("Regards")].strip() df = pd.DataFrame([Body_content]) print(df.to_string()) Ticker = df.to_string()[-82:-74] print(Ticker) price = df.to_string()[-8:] print(price) Date = df.to_string()[-21:-11] print(Date) Pkey = 116010910 #print(EMAIL_CONTNT) con = engine.connect() date = datetime.datetime.strptime(Date,"%Y-%m-%d").strftime("%d%b%Y") #print(date) #tp = "call CORE_VALUATIONS.VALUATIONS.INSERTEQCLOSINGPRICE({,to_date(({Ticker}),'ddmonyyyy'),{price},NULL,NULL);" con.execute('call CORE_VALUATIONS.VALUATIONS.INSERTEQCLOSINGPRICE(int(%s), %s,float(%s),NULL,NULL)',(Pkey,date,price))
运行效果参考截图:
修复方案
- 截取
price字段后直接做字符串替换清除所有单引号,同时清理首尾多余空白,避免切片拿到的字符串带空格导致类型转换失败:
price = df.to_string()[-8:].replace("'", "").strip()
- 建议提前在Python层完成
price的浮点数类型转换,不要在SQL语句内做隐式类型转换,降低SQL执行报错概率:
# 清洗完成后直接转成float类型 price = float(df.to_string()[-8:].replace("'", "").strip()) # 调用存储过程时移除SQL语句内套的float()转换逻辑 con.execute('call CORE_VALUATIONS.VALUATIONS.INSERTEQCLOSINGPRICE(int(%s), %s, %s,NULL,NULL)',(Pkey,date,price))
- 额外校验点:当前代码解析得到的
Ticker变量未传入存储过程,需核对存储过程入参顺序,确认是否存在漏传、错传参数的问题。
内容的提问来源于stack exchange,提问作者Rahul Vaidya
相关产品推荐
相关产品推荐

