Datetime 来自 Python 中的 string,最佳猜测字符串格式

函数要从字符串中获取日期时间,datetime.strptime(date_string, format)需要一个字符串格式作为第二个参数。有没有一种方法可以在不知道确切格式的情况下从一个字符串构建一个日期时间,并让 Python 对其进行最佳猜测?

70989 次浏览

Back before I was a python guy, I was a perl guy. One of the things that I've always missed but haven't seen anything close to it is Date::Manip. That module can extract a good timestamp from a smattering of nibbles. I almost suspect that it's author struck a deal with the Devil.

I've run across a few things that take stabs at it in Python:

If you find anything better I'd love to hear about it though.

Use the dateutil library.

I was already using dateutil as an indispensable lib for handling timezones
(See Convert UTC datetime string to local datetime and How do I convert local time to UTC in Python?)

And I've just realized it has date parsing support:

import dateutil.parser
yourdate = dateutil.parser.parse(datestring)

(See also How do I translate a ISO 8601 datetime string into a Python datetime object?)

Can get away with a simple function if only checking against dates.

def get_date(s_date):
date_patterns = ["%d-%m-%Y", "%Y-%m-%d"]


for pattern in date_patterns:
try:
return datetime.datetime.strptime(s_date, pattern).date()
except:
pass


print "Date is not in expected format: %s" %(s_date)
sys.exit(0)

You can use datefinder ,It will detect all types of natural style of dates.

import datefinder # Module used to find different style of date with time


string_value = " created 01/15/2005 by ACME inc.and associates.January 4th,2017 at 8pm"
matches = datefinder.find_dates(string_value)
for match in matches:
print("match found ",match)

Output

match found  2005-01-15 00:00:00
match found  2017-01-04 20:00:00