Extract date from string in Python

How can I extract a date from a string, for example, "monkey 2010-07-10 love banana"? Thank!

+42
python string date
Jul 18 '10 at 15:46
source share
3 answers

If the date is in a fixed form, you can simply use the regular expression to extract the date and "datetime.datetime.strptime" to parse the date:

match = re.search(r'\d{4}-\d{2}-\d{2}', text) date = datetime.strptime(match.group(), '%Y-%m-%d').date() 

Otherwise, if the date is given in arbitrary form, you cannot easily extract it.

+41
Jul 18 '10 at 15:51
source share

Using python-dateutil :

 In [1]: import dateutil.parser as dparser In [18]: dparser.parse("monkey 2010-07-10 love banana",fuzzy=True) Out[18]: datetime.datetime(2010, 7, 10, 0, 0) 

Invalid dates raise a ValueError :

 In [19]: dparser.parse("monkey 2010-07-32 love banana",fuzzy=True) # ValueError: day is out of range for month 

It can recognize dates in many formats:

 In [20]: dparser.parse("monkey 20/01/1980 love banana",fuzzy=True) Out[20]: datetime.datetime(1980, 1, 20, 0, 0) 

Note that this makes an assumption if the date is ambiguous:

 In [23]: dparser.parse("monkey 10/01/1980 love banana",fuzzy=True) Out[23]: datetime.datetime(1980, 10, 1, 0, 0) 

But the way to handle ambiguous dates is configurable:

 In [21]: dparser.parse("monkey 10/01/1980 love banana",fuzzy=True, dayfirst=True) Out[21]: datetime.datetime(1980, 1, 10, 0, 0) 
+102
Jul 18 '10 at 17:09
source share

To retrieve a date from a string in Python; The best module available is the datefinder module.

You can use it in your Python project by following these simple steps.

Step 1: Install the Dating Package

 pip install datefinder 

Step 2: use it in your project

 import datefinder input_string = "monkey 2010-07-10 love banana" # a generator will be returned by the datefinder module. I'm typecasting it to a list. Please read the note of caution provided at the bottom. matches = list(datefinder.find_dates(input_string)) if len(matches) > 0: # date returned will be a datetime.datetime object. here we are only using the first match. date = matches[0] print date else: print 'No dates found' 

note: if you expect a large number of matches; then casting to a list will not be the recommended method, since it will have a large overhead.

+11
Jul 27 '16 at 15:38
source share



All Articles