How to parse a month name in unicode to get datetime in Python 2.7

I have u'20 date 2012 'and I need to get a datetime object. (u'20 September 2012 '== u'20 January 2012')

It is very simple when you have datetime.strptime ('June 1, 2005', '% d% b% Y') But what if I have a month in different language standards?

(appengine + python 2.7)

It would be nice to have a get_month method (u'Month name in different forms), Language.RUS) → int: 'number of months' Thus, the library could process different forms of the month. For example, “June 1” and “June 1” must match the date. I use it for site scanning and parsing.

+5
source share
2 answers

, datetime, , .

-, ..

import locale, datetime

In [1]: datetime.datetime.strptime('February', '%B')
Out[1]: datetime.datetime(1900, 2, 1, 0, 0)

In [2]: locale.setlocale(locale.LC_ALL, 'de_DE')
Out[2]: 'de_DE'

In [3]: datetime.date(2008, 2, 1).strftime('%B')
Out[3]: 'Februar'

In [4]: datetime.datetime.strptime('Februar', '%B')
Out[4]: datetime.datetime(1900, 2, 1, 0, 0)
+8

All Articles