Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python inconsistent datetime parsing

Tags:

python

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?

like image 512
Hgat Avatar asked Jul 28 '26 11:07

Hgat


1 Answers

After you get the datetime object, last %z is not there. Hence, you will have to remove that when converting string back to datetime object

In [29]: format_string = "%Y-%m-%dT%H:%M:%S.%f+%z"

In [30]: now = datetime.now().strftime(format_string)

In [31]: now
Out[31]: '2020-07-20T00:29:48.228980+'

In [32]: format_string = "%Y-%m-%dT%H:%M:%S.%f+"

In [33]: datetime.strptime(now, format_string)
Out[33]: datetime.datetime(2020, 7, 20, 0, 29, 48, 228980)
like image 197
bigbounty Avatar answered Jul 30 '26 01:07

bigbounty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!