我正在嘗試在 Python 腳本中針對 Redshift DB 運行 SQL 查詢。以下結果導致錯誤“TypeError:dict不是序列”,我不明白為什么。test1 = """WITH A AS(SELECT *,CASE WHEN substring(text_value, 0, 4) LIKE ' ' THEN substring(substring(text_value, 0, 4), 3) ELSE substring(text_value, 0, 4) END AS cost_center_idFROM job_custom_fieldsWHERE key = 'cost_center')SELECT job_id,display_value, CASE WHEN cost_center_id LIKE '%T%' THEN SUBSTRING(cost_center_id, 1,1) WHEN cost_center_id LIKE '%D%' THEN SUBSTRING(cost_center_id, 1,1) WHEN cost_center_id LIKE '%P%' THEN SUBSTRING(cost_center_id, 1,1) ELSE cost_center_id END AS cost_center_idFROM A"""red_engine = create_engine('postgresql+psycopg2://org_525:[email protected]:5439/greenhouse')test = pd.read_sql_query(test1,red_engine)test非常感謝任何幫助
1 回答

繁星淼淼
TA貢獻1775條經驗 獲得超11個贊
你需要在Python中%使用轉義字符。%%
%用于 Python 中的字符串格式化。
不是這個問題的解決方案,但這里值得一提的是,在 Python 3.6 及更高版本中,有一種更推薦的使用f-strings. 例如:
name = 'eshirvana'
message = f"I am {name}"
print(message)
添加回答
舉報
0/150
提交
取消