对于多个引号字符串的处理
背景
在写sql相关的脚本时,如果遇到两层引号嵌套时,可以使用反斜杠\对于内层引号进行转义
sql = "SELECT id FROM cargo_3 where features_new like \"%updateDealModeSchema%\" "
但是在定义sql语句时遇到三重引号的情况:
sql = "SELECT id FROM cargo_3 where features_new like '%"updateDealModeSchema":6%' "
在sql执行的时候用单引号进行中间一层的引用:
那么三重引号就需要对里层的每个引号进行转义
sql = "SELECT id FROM cargo_3 where features_new like \'%\"updateDealModeSchema\":6%\' "
同时还可以用另外两种写法
sql = "SELECT id FROM cargo_3 where features_new like " + """'%"updateDealModeSchema":3%'"""
三引号中间的语句均被固定转义,无需再使用反斜杠转义符
sql = "SELECT id FROM cargo_3 where features_new like '{data}'".format(data='%"updateDealModeSchema":3%')
使用format进行变量的传递,这种方法更为灵活,且一目了然。同时还可以传多个变量"{a}{b}".fomate(a=xxx,b=xxx)
最懒的人就是整天忙得没时间学习、反思、成长的人。