[Python] Constant "except" after "try", although the date data seems to be correct

Discussão em 'Python' iniciado por Stack, Maio 21, 2025.

  1. Stack

    Stack Membro Participativo

    When processing try, an error is constantly processed.

    Help please to solve this problem, tried many formats of writing data

    def register_handlers(dp: Dispatcher):
    @dp.message(Command("start"))
    async def cmd_start(message: types.Message):
    user_context[message.from_user.id] = {"stage": "product"}
    await message.answer("Enter product name", reply_markup=ReplyKeyboardRemove())

    @dp.message(F.text)
    async def handle_text_input(message: types.Message):
    ctx = user_context.get(message.from_user.id)
    if not ctx:
    await message.answer("Type /start to get started.")
    return

    stage = ctx.get("stage")

    if stage == "product":
    ctx["product"] = message.text
    ctx["stage"] = "region"
    await message.answer("Select analysis region", reply_markup=region_keyboard)

    elif stage == "period":
    text = message.text.strip().replace("_", "-").replace("–", "-").replace("—", "-")
    match = re.match(r"(\d{2}\.\d{2}\.\d{4})\s*-\s*(\d{2}\.\d{2}\.\d{4})", text)
    if not match:
    await message.answer("Invalid format. Use DD.MM.YYYY-DD.MM.YYYY")
    return
    ** try:
    start_date_str, end_date_str = match.groups()
    start_date = datetime.strptime(start_date_str, "%d.%m.%Y")
    end_date = datetime.strptime(end_date_str, "%d.%m.%Y")
    ctx["period"] = (start_date, end_date)
    ctx["stage"] = "done"
    await generate_report(message, ctx)
    except Exception as e:
    await message.answer("Error processing dates. Try again.")**

    else:
    await message.answer("Please follow the steps. Type /start to begin..")


    I enter the date in the following format: "21.05.2024-21.05.2025".

    But theris always "Error processing dates. Try again." tried many formats of writing data

    Continue reading...

Compartilhe esta Página