【问题标题】:Airflow: Convert one digit to two digit with execution_date气流:使用 execution_date 将一位数转换为两位数
【发布时间】:2022-01-20 17:13:06
【问题描述】:

我正在尝试将我的气流日期从一位数转换为两位数。在我使用{{ execution_date.month }} 的那一刻,我的月份变成了1,但希望它变成01

我已尝试使用{{ execution_date.month.strftime("%m") }},但收到错误:

int object' 没有属性 'strftime

【问题讨论】:

  • 试试execution_date.strftime("%m")

标签: python date airflow


【解决方案1】:

你可以这样做:

from datetime import datetime
from airflow.models import DAG
from airflow.operators.bash import BashOperator

with DAG(
    dag_id="bash_example",
    schedule_interval=None,
    start_date=datetime(2022, 1, 19),
    catchup=False
) as dag4:

    BashOperator(task_id="task", bash_command="echo {{ execution_date.strftime('%m') }}")

或者

from datetime import datetime
from airflow.decorators import dag
from airflow.operators.bash import BashOperator

@dag(schedule_interval=None, start_date=datetime(2022, 1, 19), catchup=False)
def bash_example():
    BashOperator(task_id="task", bash_command="echo {{ execution_date.strftime('%m') }}")


dag = bash_example()

渲染:

日志:

【讨论】:

    猜你喜欢
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    • 2020-12-07
    • 1970-01-01
    • 1970-01-01
    • 2011-01-02
    • 1970-01-01
    • 2020-12-28
    相关资源
    最近更新 更多