python - Escape single quote when using Pandas with tuple for IN statement -
i trying use in statement psycopg2. passing tuple, 1 of items string contains single quote (ex: l'enfant plaza).
when converting tuple, python automatically adds double quotes (ex: ('union station', "l'enfant plaza", 'dupont cir'))
therefore, when execute sql query, getting error:
dataframe = pd.read_sql("""select * trips originlocation in {} """.format(origin_locations_tuple), connection) error: column "l'enfant plaza" not exist
clodoaldo's advice put me on right track. using pandas query parameters solved it. working version looks this:
sql = """select * final_trips_prod (dateoftrip + origintime) between (timestamp %(datetime_selected)s - interval '3 hours') , (timestamp %(datetime_selected)s + interval '3 hour') , originlocation in %(origin_locations)s , destinationlocation in %(destination_locations)s """ dataframe = pd.read_sql_query(sql = sql, con = connection, params = { 'datetime_selected': datetime_selected, 'origin_locations': origin_locations, 'destination_locations': destination_locations})
Comments
Post a Comment