I am trying to read a date string in a particular format.
First let's generate a string of the current time in the required format.
In : format_string = "%Y-%m-%dT%H:%M:%S.%f+%z"
In : now = datetime.now().strftime(format_string)
In : print(now)
Out: '2020-07-19T19:12:09.453475+'
Now, let's try to convert it back to a datetime object.
In : datetime.strptime(now, format_string)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-15-32937025edd1> in <module>
----> 1 datetime.strptime("2020-07-19T19:12:09.453475+", "%Y-%m-%dT%H:%M:%S.%f+%z")
/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/_strptime.py in _strptime_datetime(cls, data_string, format)
575 """Return a class cls instance based on the input string and the
576 format string."""
--> 577 tt, fraction, gmtoff_fraction = _strptime(data_string, format)
578 tzname, gmtoff = tt[-2:]
579 args = tt[:6] + (fraction,)
/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/_strptime.py in _strptime(data_string, format)
357 if not found:
358 raise ValueError("time data %r does not match format %r" %
--> 359 (data_string, format))
360 if len(data_string) != found.end():
361 raise ValueError("unconverted data remains: %s" %
ValueError: time data '2020-07-19T19:12:09.453475+' does not match format '%Y-%m-%dT%H:%M:%S.%f+%z'
The error is about the format, which is exactly the same.
What is going on here?