You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python f-string中传递大括号{、}作为字符串?

在Python F-string中传递SQL里的大括号字符

要在f-string里保留SQL语句中的{和}作为普通字符串,你需要把每个大括号重复写两次——也就是用{{}}代替单个的{}。

f-string的语法规则是:单个{}会被当作变量占位符解析,而双重{{或}}会被转义成普通的单个大括号字符。修改你的代码如下:

query = f"""Select fd.serial_number,txidkey,cast(replace(replace(data->>'Hourly Status','{{',''),'}}','') as text) as description,TO_TIMESTAMP(TIME/1000+19800) as date_time,time,total_min from filter_data fd , total_sum ts
where fd.serial_number = ts.serial_number
and time between {yesterday10PM*1000} and {today6AM*1000}"""

另外注意你原代码末尾多写了三个单引号,已经帮你去掉了,避免字符串闭合错误。

内容的提问来源于stack exchange,提问作者zircon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 05:42:41