简体   繁体   中英

unsupported format character 'O' (0x4f) at index 79

I'm getting the below error while executing a query using Psycopg2 module. I have executed the same query in PgAdmin 4, and it is working fine and giving me results .The error occurs when i execute it via Python.

Can anyone suggest me how to resolve this?

below is the code:

for new_ticket in zenpy_client.search(type='ticket', status='open',requester='je.rr.y@gmail.com'):
    #new_ticket.assignee = modified_user
     #print(new_ticket.requester.email)

     psql_command=""" WITH CTE AS (select cast(origin as text) as origin, MAX(case when name like '%OUT%' THEN name else ''end) as out_ref
                                    , MAX(case when name like '%PICK%' THEN name else ''end) as pick_ref
                                    , MAX(case when name like '%PICK%' THEN state else ''end) as pick_state
                                        , MAX(case when name like '%OUT%' THEN state else ''end) as out_state
                     from dl_odoo.stock_picking 
                     GROUP BY origin)
               select o.customer_email,o.order_number,o.order_state_1,o.order_state_2,o.order_subtotal_net_after_discount as revenue
                 ,created_at_order,c.out_ref as reference,o.payment_method,c.pick_ref,c.pick_state,c.out_ref,c.out_state
               from ol.orders o 
                       LEFT JOIN CTE c ON c.origin=o.order_number 
                   where
                       customer_email='%s' order by o.created_at_order desc limit 3
                   ;"""
     try:
         s=''
         print(new_ticket.requester.email)
         print(psql_command)
         try:
             psql_cursor.execute(psql_command %new_ticket.requester.email)
         except (Exception, psycopg2.Error) as error:
             print ("ssss")
             print (error)

You need to escape the percent signs in the string. Python is interpreting them as if they are printf-like format specifiers. Replace each % with %% and it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM