简体   繁体   中英

Python does not match format '%Y-%m-%dT%H:%M:%S%Z.%f'

I tried to convert string into a datetime object in Python, and I can't find anything wrong with my format %Y-%m-%dT%H:%M:%S%Z.%f .

import datetime
datetime.datetime.strptime('2019-11-19T17:22:23.171833', '%Y-%m-%dT%H:%M:%S%Z.%f')


  File "/var/lang/lib/python3.7/_strptime.py", line 577, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
  File "/var/lang/lib/python3.7/_strptime.py", line 359, in _strptime
    (data_string, format))

ValueError: time data '2019-11-19T17:22:23.171833' does not match format '%Y-%m-%dT%H:%M:%S%Z.%f'

Your format should be:

'%Y-%m-%dT%H:%M:%S.%f'

This looks like isoformat, so consider to use the special case convenience :

>>> from datetime import datetime
>>> datetime.fromisoformat('2019-11-19T17:22:23.171833')
datetime.datetime(2019, 11, 19, 17, 22, 23, 171833)

%Z - Time zone name (empty string if the object is naive).

Should this be included?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM