Striptime Python partial matching -


this question has answer here:

i have follwoing format date 2014-02-23t18:42:26.000009. want extract date, have used following code:

from datetime import datetime date_object = datetime.strptime('2014-02-23t18:42:26.000009', '%y-%m-%d') 

however, got following error message:

traceback (most recent call last): file "c:/users/d774911/pycharmprojects/globaldata/test2.py", line 4, in <module> date_object = datetime.strptime('2014-02-23t18:42:26.000009', '%y-%m-%d') file "c:\python34\lib\_strptime.py", line 500, in _strptime_datetime tt, fraction = _strptime(data_string, format) file "c:\python34\lib\_strptime.py", line 340, in _strptime data_string[found.end():]) valueerror: unconverted data remains: t18:42:26.000009 

any ideas?

you need add remains of string date format.so replace '%y-%m-%d' with '%y-%m-%dt18:42:26.000009' :

>>> date_object = datetime.strptime('2014-02-23t18:42:26.000009', '%y-%m-%dt18:42:26.000009') >>> date_object datetime.datetime(2014, 2, 23, 0, 0) 

or more general way can use dateutil.parser :

>>> dateutil.parser import parse >>> parse('2014-02-23t18:42:26.000009', fuzzy=true) datetime.datetime(2014, 2, 23, 18, 42, 26, 9) 

and if want date :

>>> parse('2014-02-23t18:42:26.000009', fuzzy=true).date() datetime.date(2014, 2, 23) 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -