Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL case when X LIKE '%T%' in Python script resulting in "TypeError: dict is not a sequence"

Tags:

python

sql

I am attempting to run a SQL query against a Redshift DB in a Python script. The following results in an error "TypeError: dict is not a sequence" and I cannot figure out why.

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:[email protected]:5439/greenhouse')
test = pd.read_sql_query(test1,red_engine)
test

Any assistance is much appreciated

like image 705
aguadamuz Avatar asked Dec 12 '25 02:12

aguadamuz


1 Answers

you need to escape % character using %% in Python.

% is used for string formatting in Python.

Not a solution to this problem but it's worth mentioning here that in Python 3.6 and later, there is a recommended way of string formatting using f-strings. For example:

name = 'eshirvana'
message = f"I am {name}"
print(message)
like image 142
eshirvana Avatar answered Dec 13 '25 15:12

eshirvana



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!