python 参数命名的坑

在处理一批数据时,时间参数命名为如下红体字这样带下划线的格式,

def get_data( dt_start,dt_end):
with execute_sql() as getData:
s = f"select * into temp_new.dbo.online_临时表 from (select distinct b.一级来源类型,isnull(a.总量,0)数量 from eccrc_rep.dbo.pg_cust_analysis_all b left join (SELECT 一级来源类型,COUNT(pg_cust_id)总量 FROM eccrc_rep.dbo.pg_cust_analysis_all WHERE 客户状态='0' AND 工商类型='标准企业工商' AND 创建年月>='{ dt_start}' AND 创建年月<= '{dt_end }' GROUP BY 一级来源类型)a on b.一级来源类型=a.一级来源类型 where b.一级来源类型 in ('活动管理平台','市场BD','销售录入','信誉','资源中心','总部CSC下发'))c;"
print(s)
getData.execute(s)
 
结果当脚本跑到这个函数是,就会提示如下错误:

(venv) PS D:\Project\python\webfirst> python month.py
File "month.py", line 74
SyntaxError: Non-UTF-8 code starting with '\xe5' in file month.py on line 74, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
(venv) PS D:\Project\python\webfirst>

这是为啥呢?怎么解决呢?

这是个坑,python中的 执行的sql语句中 ‘创建年月>='{dt_start}' AND 创建年月<='{dt_end}' ’参数不能带特殊符号,例如 '_' ,不能带下划线,命名参数时要注意。不然会出错。

将dt_start,dt_end这两个参数改为start,end后,脚本运行成功。

猜你喜欢

转载自www.cnblogs.com/cheng-cheng/p/10000810.html