Skip to content
Advertisement

IllegalMonthError in Python datefinder

I am trying to extract dates from email texts using datefinder python library.

Below is a the code snippet of what I am trying to do.

JavaScript

datefinder tries to construct all the numbers in the email to dates. I get lot of false positives. I can remove those using some logic. But i get IllegalMonthError in some email and i am unable to go past the error and retrieve dates from other emails. Below is the error

JavaScript

Suppose if i am getting this error in the 5th email, I will not be able to retrieve dates from 5th onwards. How to bypass this error, remove the entries causing this error and retrieve all other dates?

Thanks in Advance

Advertisement

Answer

Use a try/except block:

JavaScript

Update from the edits:

Notice that the traceback shows

JavaScript

that the exeption is raised here. Indeed, checking the documentations for find_dates, you see that find_dates returns a generator:

Returns a generator that produces datetime.datetime objects, or a tuple with the source text and index, if requested

So the actual parsing of the date is not done when you call find_dates, but when you iterate over the results. This makes it trickier to wrap in a try/catch, as you have to iterate over the generator item by item, each in a separate try/catch block:

JavaScript

You can do with m whatever you need.

Cheers!

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement