气流中的Jinja模板和格式化文本

2024-09-27 00:18:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图运行一条SQL语句,由Affaiw呈现,但我也试图在该语句中包含一个从Python传入的变量。SQL语句只是一个where子句,在WHERE之后,我试图添加一个datetime,减去几秒钟:

f" ' {{ ts - macros.timedelta(seconds={lower_delay} + 1) }} ' "

所以我想计算双大括号中的内容并在气流中渲染,但我想在它执行此操作之前传入名为lower_delay的变量。我尝试过在lower_delay和整个字符串周围使用零、一或两个额外的大括号进行不同的组合,但每次似乎都会出现不同的错误

传递这个lower_delay变量(它只是一个数字)的正确方法是什么


Tags: sqldatetime语句大括号wherelowermacrostimedelta
1条回答
网友
1楼 · 发布于 2024-09-27 00:18:22

Jinja模板需要两个大括号,当您使用f-strings或str.format时,它将在渲染时用一个大括号替换两个大括号:

Format strings contain “replacement fields” surrounded by curly braces {}. Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. If you need to include a brace character in the literal text, it can be escaped by doubling: {{ and }}.

因此,以下措施应该有效:

f" ' {{{{ ts - macros.timedelta(seconds={lower_delay} + 1) }}}} ' "

相关问题 更多 >

    热门问题