Python Does Not Match Format '%Y-%M-%Dt%H:%M:%S%Z.%F'

'2019-07-17T00:00:00.000000000Z' does not match format '%Y-%m-%dT%H:%M:%S.%fZ'

It is a problem you can find out by yourselves simply by adjusting the date and format:

Fail:

d = datetime.strptime("2019-07-17T00:00:00.000000000Z", "%Y-%m-%dT%H:%M:%S.%fZ")

Success:

d = datetime.strptime("2019-07-17T00:00:00", "%Y-%m-%dT%H:%M:%S")

Which means the problem comes from the %fZ part.

From Python document, %f means microsecond which means 6 digits.

d = datetime.strptime("2019-07-17T00:00:00.000000Z", "%Y-%m-%dT%H:%M:%S.%fZ")

That's the above will work


Edit: There are two choices you can have:

  1. If the incoming string always ends with 3 zeros for the nanosecond part, you can adjust your format to "%Y-%m-%dT%H:%M:%S.%f000Z"

  2. If you are not sure, then you can just trim off last 3 digits from the input string

One way you can do:

input_string = "2019-07-17T00:00:00.000000000Z"
result = datetime.strptime(input_string[:-4], "%Y-%m-%dT%H:%M:%S.%f")

Here I excluded the last 4 characters (000Z) from the string and the format is updated accordingly to remove the matching of last Z.

time data does not match format '%y-%m-%d'

Use Y for match YYYY format, because y is for YY format of year:

self.train_date = datetime.datetime.strptime(train_date,'%Y-%m-%d').date()

ValueError: time data '02/03/2022' does not match format '%d/%m/%y '

To point out the year you need to use %Y and also there is an additional space at the end of the format you gave that it's not present in the date. Try with date = datetime.strptime(date, '%d/%m/%Y')



Related Topics



Leave a reply



Submit