我正在尝试在 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:LZynsTS56PqPlHVhIIgUdDf1c1wuW4gD@redshift.greenhouse.io:5439/greenhouse')test = pd.read_sql_query(test1,red_engine)test非常感谢任何帮助
1 回答
![?](http://img1.sycdn.imooc.com/5333a1d100010c2602000200-100-100.jpg)
繁星淼淼
TA贡献1775条经验 获得超11个赞
你需要在Python中%使用转义字符。%%
%用于 Python 中的字符串格式化。
不是这个问题的解决方案,但这里值得一提的是,在 Python 3.6 及更高版本中,有一种更推荐的使用f-strings. 例如:
name = 'eshirvana'
message = f"I am {name}"
print(message)
添加回答
举报
0/150
提交
取消