1. 程式人生 > >Python中: unsupported format character ''' (0x27)

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%';