Python –值错误:索引968

时间:2019-08-26 18:01:13

标签: python sql snowflake-data-warehouse

关于Stackoverflow有很多类似的问题,我在这里已经研究了一段时间。但是,对于我遇到的特定问题,我仍然找不到解决方案。

我正在使用Python Snowflake连接器使用sql文件提取数据。我的文件目录如下:

SQL
   pay_gbd.sql
data_extract.py

pay_gbd.sql如下所示,带有两个参数-start_pickup_data和end_pickup_date:

WITH
payment as
(
    select
          confirmation_number
        , payment_statuses
        , case
            when lower(payment_statuses) like '%%show%%' then 'NOSHOW'
            when lower(payment_statuses) like '%%cancel%%' then 'CANCEL'
            else 'SHOW'
          end as payment_status
    from payment_table
),

rawData as 
(   select *
    from booking_table g
    where
        g.pu_timestamp >= %(start_pickup_date)s and g.pu_timestamp < %(end_pickup_date)s
        and g.supplier_car_days > 0)

select *
from rawData;

如您所见,我使用'%% s %%'是为了避免错误。

data_extract.py具有以下代码:

def executeSQLScriptsFromFile(filepath, param_dict):

    ctx = snowflake.connector.connect(
        user='USER_NAME',
        account='SECRET_1',
        region='us-east-1',
        warehouse='SECRET_2',
        database='SECRET_3',
        role='SECRET_4',
        password='SECRET_5')

    fd = open(filepath, 'r')
    query = fd.read()
    fd.close()
    print(query)
    cs = ctx.cursor()
    try:
        cur = cs.execute(query, param_dict)
        df = pd.DataFrame.from_records(iter(cur), columns=[x[0] for x in cur.description])
    finally:
        cs.close()
    ctx.close()

    return df

def extract_pay_gbd(start_pickup_date, end_pickup_date):
    pay_gbd_sqlpath = os.path.join(os.getcwd(), 'sql/pay_gbd.sql')

    print('Start extracting pay_gbd data from Snowflake, pickup date range: {} to {}'.format(start_pickup_date,                                                                                            end_pickup_date))
    param_dict = {'start_pickup_date': start_pickup_date, 'end_pickup_date': end_pickup_date}
    pay_gbd = executeSQLScriptsFromFile(pay_gbd_sqlpath, param_dict)

    return pay_gbd

但是,当我运行 extract_pay_gbd 函数时,总是出现以下错误:

  File "C:\Users\...\data_extract.py", line 45, in executeSQLScriptsFromFile
    cur = cs.execute(query, param_dict)
  File "C:\Users\...\snowflake\connector\cursor.py", line 458, in execute
    query = command % processed_params
ValueError: unsupported format character ',' (0x2c) at index 968

print(query)的输出看起来与.sql文件中的查询完全相同:

WITH
payment as
(
    select
          confirmation_number
        , payment_statuses
        , case 
            when lower(payment_statuses) like '%%show%%' then 'NOSHOW'
            when lower(payment_statuses) like '%%cancel%%' then 'CANCEL'
            else 'SHOW'
          end as payment_status
    from payment_table
),

rawData as 
(   select *
    from booking_table g
    where
        g.pu_timestamp >= %(start_pickup_date)s and g.pu_timestamp < %(end_pickup_date)s
        and g.supplier_car_days > 0)

select *
from rawData;

任何建议都将受到赞赏!

1 个答案:

答案 0 :(得分:0)

按照@JohnGordon的提示,我找到并解决了这个问题。希望这里的解决方案对Snowflake Python Connector的新手有所帮助。

问题是我在sql文件的注释部分中有一个'%'。删除它,代码将运行完美。