문제

Does anybody know a Python module to easily extract temporal expressions such as 12/01/2011 and Monday, 12/3/1987 or others (in English format)?

Would like to avoid to build a huge set of regex.

도움이 되었습니까?

해결책

In the standard library you won't find something comprehensive, but if you install dateutils, you can do this:

>>> from dateutils import parser
>>> parser.parse('January 12, 2012').strftime('%s')
'1326315600'
>>> parser.parse('01/12/2012').strftime('%s')
'1326315600'
>>> parser.parse('Sunday, 16/09/2012').strftime('%s')
'1347742800'

Martijn had a point, so here is the normal representation - which is datetime object:

>>> parser.parse('Sunday, 16/09/2012')
datetime.datetime(2012, 9, 16, 0, 0)
>>> parser.parse('01/12/2012')
datetime.datetime(2012, 1, 12, 0, 0)
>>> parser.parse('January 12, 2012')
datetime.datetime(2012, 1, 12, 0, 0)

다른 팁

Use the standard datetime.datetime.strptime().

Example:

In : datetime.datetime.strptime("12/01/2012", "%m/%d/%Y")
Out: datetime.datetime(2012, 12, 1, 0, 0)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top