Convert time strings into datetime format coudd be very tedious, because we need to make sure the every new data
follow the exact format required by the conversion functions, otherwise it will raise error.
One good way is to user the dateutil.parser package, which is much more flexiable. It can also support some fuzziness.
With this package, even some data has slightly different formats than we expect, it will not cause error.
The traditional way to convert time string to datatime format
import datetime |
2022
2022
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-20-a29c39b3ca30> in <module>
15 # in the above example if there is a new item that doesn't follow the milliseconds format, it will raise error
16 item = '2022/01/01 10:49:12'
---> 17 result = datetime.datetime.strptime(item, "%Y/%m/%d %H:%M:%S.%f")
18
19 print(result.year)
D:\Anaconda3\lib\_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,)
D:\Anaconda3\lib\_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 '2022/01/01 10:49:12' does not match format '%Y/%m/%d %H:%M:%S.%f'
use parser in dateutil package, a more stable way
from dateutil.parser import * |
2022
2022