猿问

Python 脚本中 X LIKE '%T%' 时的 SQL 情况导致

我正在尝试在 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_id

FROM job_custom_fields

WHERE 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_id

FROM A

"""


red_engine = create_engine('postgresql+psycopg2://org_525:LZynsTS56PqPlHVhIIgUdDf1c1wuW4gD@redshift.greenhouse.io:5439/greenhouse')

test = pd.read_sql_query(test1,red_engine)

test

非常感谢任何帮助


侃侃无极
浏览 94回答 1
1回答

繁星淼淼

你需要在Python中%使用转义字符。%%%用于 Python 中的字符串格式化。不是这个问题的解决方案,但这里值得一提的是,在 Python 3.6 及更高版本中,有一种更推荐的使用f-strings. 例如:name = 'eshirvana'message = f"I am {name}"print(message)
随时随地看视频慕课网APP

相关分类

Python
我要回答