Python中: unsupported format character ''' (0x27)

       python中,拼接字符串,使用'''符号拼接成下面的内容:

select mobile user_device from user_info 
where 1=1
and product_name not like '%大王咔%'
and pay_type='2'
and date_time like '201811%';

      最初使用下面的语法,报 unsupported format character ''' (0x27)错误:

big_card = '''select mobile user_device from user_info 
where 1=1
and product_name not like '%%s%'
and pay_type='2'
and date_time like '%s%';
''' % (product_name,pay_type,date_time)

    经过查询发现,当“%s”前后有“%”的时候,想要将其当做“%”的字符使用,需要用“%%”表示,因此改动方案如下:

big_card = '''select mobile user_device from user_info 
where 1=1
and product_name not like '%%%s%%'
and pay_type='2'
and date_time like '%s%%';
''' % (product_name,pay_type,date_time)

     这样之后,就不会报错了,顺利拼接成下面的字符串:

select mobile user_device from user_info 
where 1=1
and product_name not like '%大王咔%'
and pay_type='2'
and date_time like '201811%';

猜你喜欢

转载自blog.csdn.net/hefrankeleyn/article/details/84986982