[Python] Python ValueError: ':' is a bad directive in format '%Y-%m-%d%X%:z'

Discussão em 'Python' iniciado por Stack, Março 28, 2025.

  1. Stack

    Stack Membro Participativo

    I'm attempting to parse HTML time strings in Python:

    from datetime import datetime

    input = "2025-03-24T07:01:53+00:00"
    output = datetime.strptime(input, "%Y-%m-%d%X%:z")
    print(output)


    Running this with Python 3.13 returns the following error:

    Traceback (most recent call last):
    File "/Users/dread_pirate_roberts/html_time_converter/main.py", line 69, in <module>
    output = datetime.strptime(input, "%Y-%m-%d%X%:z")
    File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/_strptime.py", line 674, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
    ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
    File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/_strptime.py", line 445, in _strptime
    raise ValueError("'%s' is a bad directive in format '%s'" %
    (bad_directive, format)) from None
    ValueError: ':' is a bad directive in format '%Y-%m-%d%X%:z'


    This doesn't make sense to me because %:z was added in Python 3.12.

    Continue reading...

Compartilhe esta Página