Is there a Python module to parse month or day开发者_开发技巧 references in a given string?
You could try using dateutil.parser
, e.g.:
>>> from dateutil import parser
>>> parser.parse("There's a date 09-21 in here", fuzzy=True)
datetime.datetime(2011, 9, 21, 0, 0)
... with the fuzzy=True
option. The documentation is here.
Are any other characters in the string or just numbers which says the date? If you have a date string and you want to create a date which you can format, try this:
import time
time.strptime(datestring,"%d.%m.%Y %H:%M:%S") # for example 17.03.2011 10:05:15
You can also create an UNIX timestamp
timestamp = int(time.mktime(time.strptime(date,"%d.%m.%Y %H:%M:%S")))
You can use wide range of formats, more info here
Another possibility is to combine this with a regular expression on the string, but Mark's module would be much better for this.
精彩评论