开发者

Is there a Python module to detect month or day in string?

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜