Python Datetime Strptime Error: '-' is a bad directive in the format '% -m -% - d-% y% -H:% M:% S'

I know that the same questions were asked, but they seem to be related to the way datetime deals (or doesn't) with time zones.

The setup is a bit complicated and probably not related to the problem, but I thought it was important to include the code as is, so a bit of background:

I have a dictionary of arrays. Each of these arrays represents an “attempt” of the same person, but occurs at different times. Ultimately, I will look for the earliest of these dates. This might be a bit of a workaround, but I convert all dates to datetime objects, find the earliest, and then just use this index to pull out the first try:

Here is the code for setting up this datetimes attempt array:

for key in duplicates_set.keys(): attempt_dates = [datetime.strptime(attempt['Attempt Date'], "%-m-%-d-%y %-H:%M:%S") for attempt in duplicates_set[key]] 

Here is a format that looks like one of the source lines:

12-5-2016 3:27:58 PM

What I get is:

 ValueError: '-' is a bad directive in format '%-m-%d-%y %-H:%M:%S' 

I assume that referring to the dashes placed before the "m", "d" and "H" because they are non-zero decimal places. Why does this tell me that?

+5
source share
1 answer

%-* - skip add-on - this is a GNU libc extension. It is not part of POSIX strftime and therefore cannot be guaranteed to be ported to systems where your calls during formatting are ultimately not supported by the GNU strftime C library.

The Python datetime module documentation explicitly indicates the supported format strings, and this extension is not specified. Thus, although it is supported in GNU date and GNU strftime() , it is not available in Python datetime .

+6
source

Source: https://habr.com/ru/post/1261410/


All Articles