Background:
I have a lot of log data, every log has a date string, and I need to convert it to a DateTime format.
The problem is that there are a variety of string formats in these logs, with 2017-05-25t05:27:30.313292255z, 2016-07-01t00:00:00 and other formats I haven't seen yet.
At first I wrote a long list of if else to determine the format, but there was always something I missed.
Finally on the Internet a search, found dateutil.parser.parse. You can convert a string to a DateTime format without having to specify the format.
Import Datetimeimport dateutil.parserdef GetDateTime (s): d = dateutil.parser.parse (s) return D
Note: I tried the "19/may/2017:04:10:06 +0000" unexpectedly failed--! Maybe this function only knows the numbers and doesn't recognize the letters.
Forward ' https://www.cnblogs.com/dplearning/p/7388659.html
"Python" Uniform Conversion date format Dateutil.parser.parse