Normalize different date data into single format in Python -
this question has answer here:
i analyzing dateset contains many different date types
12/31/1991 december 10, 1980 september 25, 1970 2005-11-14 december 1990 october 12, 2005 1993-06-26
is there way normalize date data single format 'yyyy-mm-dd' ? familiar datetime package in python, what's best way approach problem can handle different date types.
if okay using library, can use dateutil
library (i believe comes installed python 3 +) , dateutil.parser.parse
function, , parse dates datetime objects, , use datetime.datetime.strftime()
parse them strings in format - 'yyyy-mm-dd'
. example -
>>> s = """12/31/1991 ... december 10, 1980 ... september 25, 1970 ... 2005-11-14 ... december 1990 ... october 12, 2005 ... 1993-06-26""" >>> dateutil import parser >>> in s.splitlines(): ... d = parser.parse(i) ... print(d.strftime("%y-%m-%d")) ... 1991-12-31 1980-12-10 1970-09-25 2005-11-14 1990-12-10 2005-10-12 1993-06-26
a thing note, dateutil.parser.parse
use current datetime make parts of datetime if missing in string (as can seen above in parsing of 'december 1990'
, got parsed - 1990-12-10
10
current date) .
Comments
Post a Comment