ValueError: unsupported format character ''' (0x27) at index 119

刘超 10天前 ⋅ 140 阅读   编辑

一、描述

  在python中封装以下查询语句,如下

 sql = """select day, '%s' as advertiser_id, sum(revenue) from revshare_revenue.adx_bidding_data_view where advertiser like '%%s%' and format_date('%%Y%%m%%d',day) between '%s' and '%s'  group by day order by day"""%(col,cond,args["startDate"],args["endDate"])

  执行后报如下错误

ValueError: unsupported format character ''' (0x27) at index 119

二、解决方法

  看起来python正在将%解释为类似于printf的格式字符。这里%就是普通字符,没有什么特殊含义,所以需要转义,将%换成%%,如下

 sql = """select day, '%s' as advertiser_id, sum(revenue) from revshare_revenue.adx_bidding_data_view where advertiser like '%%%s%%' and format_date('%%Y%%m%%d',day) between '%s' and '%s'  group by day order by day"""%(col,cond,args["startDate"],args["endDate"])

三、参考文章

  1、https://stackoverflow.com/questions/11695801/python-valueerror-unsupported-format-character-0x27-at-index-1

  2、https://stackoverflow.com/questions/8856523/valueerror-unsupported-format-character-while-forming-strings


注意:本文归作者所有,未经作者允许,不得转载

全部评论: 0

    我有话说: