原始 f 字符串中不支持的格式字符

时间:2021-02-02 07:30:18

标签: python

我正在尝试使用 sqlalchemy 执行以下查询。

engine=sqlalchemy.create_engine('mysql+pymysql://root:root@localhost:3306/cst')

cst_delete_query = rf"""delete from customer where STATUS like '%CT%'"""

with engine.connect().execution_options(autocommit=True) as conn:
     conn.execute(cst_delete_query)

这会导致以下错误:

ValueError: unsupported format character 'C' (0x43) at index 41

1 个答案:

答案 0 :(得分:1)

百分比字符是一个字符串运算符,应该转义以在字符串中使用。您可以使用第二个 % 将其转义。

cst_delete_query = rf"DELETE FROM customer WHERE status LIKE '%%CT%%'"
相关问题