Python datefinder中的IllegalMonthError

Python datefinder中的IllegalMonthError,python,python-3.x,date,jupyter-notebook,datefinder,Python,Python 3.x,Date,Jupyter Notebook,Datefinder,我正在尝试使用datefinderpython库从电子邮件文本中提取日期 下面是我正在尝试做的代码片段 import datefinder #body has list of email texts email_dates=[] for b in body: dates = datefinder.find_dates(b) date = [] for d in dates: date.append(d) email_dates.append

我正在尝试使用
datefinder
python库从电子邮件文本中提取日期

下面是我正在尝试做的代码片段

import datefinder

#body has list of email texts

email_dates=[]
for b in body: 
    dates = datefinder.find_dates(b)
    date = []
    for d in dates:
        date.append(d)

    email_dates.append(date)
datefinder尝试将电子邮件中的所有数字构造为日期。我有很多误报。我可以使用一些逻辑删除这些。但是我在一些电子邮件中收到了
IllegalMonthError
,我无法越过错误并从其他电子邮件中检索日期。下面是错误

---------------------------------------------------------------------------
IllegalMonthError                         Traceback (most recent call last)
c:\python\python38\lib\site-packages\dateutil\parser\_parser.py in parse(self, timestr, default, ignoretz, tzinfos, **kwargs)
    654         try:
--> 655             ret = self._build_naive(res, default)
    656         except ValueError as e:

c:\python\python38\lib\site-packages\dateutil\parser\_parser.py in _build_naive(self, res, default)
   1237 
-> 1238             if cday > monthrange(cyear, cmonth)[1]:
   1239                 repl['day'] = monthrange(cyear, cmonth)[1]

c:\python\python38\lib\calendar.py in monthrange(year, month)
    123     if not 1 <= month <= 12:
--> 124         raise IllegalMonthError(month)
    125     day1 = weekday(year, month, 1)

IllegalMonthError: bad month number 42; must be 1-12

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-39-1fbacc8ca3f6> in <module>
      7     dates = datefinder.find_dates(b)
      8     date = []
----> 9     for d in dates:
     10         date.append(d)
     11 

c:\python\python38\lib\site-packages\datefinder\__init__.py in find_dates(self, text, source, index, strict)
     30         ):
     31 
---> 32             as_dt = self.parse_date_string(date_string, captures)
     33             if as_dt is None:
     34                 ## Dateutil couldn't make heads or tails of it

c:\python\python38\lib\site-packages\datefinder\__init__.py in parse_date_string(self, date_string, captures)
    100         # otherwise self._find_and_replace method might corrupt them
    101         try:
--> 102             as_dt = parser.parse(date_string, default=self.base_date)
    103         except (ValueError, OverflowError):
    104             # replace tokens that are problematic for dateutil

c:\python\python38\lib\site-packages\dateutil\parser\_parser.py in parse(timestr, parserinfo, **kwargs)
   1372         return parser(parserinfo).parse(timestr, **kwargs)
   1373     else:
-> 1374         return DEFAULTPARSER.parse(timestr, **kwargs)
   1375 
   1376 

c:\python\python38\lib\site-packages\dateutil\parser\_parser.py in parse(self, timestr, default, ignoretz, tzinfos, **kwargs)
    655             ret = self._build_naive(res, default)
    656         except ValueError as e:
--> 657             six.raise_from(ParserError(e.args[0] + ": %s", timestr), e)
    658 
    659         if not ignoretz:

TypeError: unsupported operand type(s) for +: 'int' and 'str'
---------------------------------------------------------------------------
IllegalMonthError回溯(最近一次呼叫最后一次)
解析中的c:\python\python38\lib\site packages\dateutil\parser\\u parser.py(self、timestr、default、ignoretz、tzinfos、**kwargs)
654试试:
-->655 ret=self.\u build\u naive(res,默认值)
656除e值错误外:
c:\python\python38\lib\site packages\dateutil\parser\\u parser.py in\u build\u naive(self,res,默认值)
1237
->1238如果cday>monthrange(cyear,cmonth)[1]:
1239回复['day']=蒙特兰奇(cyear,cm月)[1]
蒙特兰奇的c:\python\python38\lib\calendar.py(年、月)
123如果d in日期不是1 9:
10日期。附加(d)
11
c:\python\python38\lib\site packages\datefinder\\uuuuu init\uuuuuu.py in find\u dates(self、text、source、index、strict)
30         ):
31
--->32 as_dt=self.parse_date_字符串(日期字符串,捕获)
33如果as_dt为无:
Dateutil对此一无所知
c:\python\python38\lib\site packages\datefinder\\uuuuuu init\uuuuuuuu.py在parse\u date\u字符串中(self,date\u字符串,捕获)
100#否则self._find_和_replace方法可能会损坏它们
101尝试:
-->102 as_dt=parser.parse(日期字符串,默认值=self.base_日期)
103除外(ValueError,OverflowerError):
104#替换dateutil有问题的令牌
解析中的c:\python\python38\lib\site packages\dateutil\parser\\u parser.py(timestr、parserinfo、**kwargs)
1372返回解析器(parserinfo).parse(timestr,**kwargs)
其他:
->1374返回DEFAULTPARSER.parse(timestr,**kwargs)
1375
1376
解析中的c:\python\python38\lib\site packages\dateutil\parser\\u parser.py(self、timestr、default、ignoretz、tzinfos、**kwargs)
655 ret=self.\u build\u naive(res,默认值)
656除e值错误外:
-->657.从(ParserError(e.args[0]+“:%s”,timestr),e)中提升
658
659如果不是ignoretz:
TypeError:不支持+:“int”和“str”的操作数类型
假设我在第5封电子邮件中收到此错误,我将无法检索第5封以后的日期。如何绕过此错误,删除导致此错误的条目并检索所有其他日期


提前感谢

使用
尝试/例外
块:

try:
    datefinder.find_dates(b)
except IllegalMonthError as e:
    # this will print the error, but will not stop the program
    print(e)
except Exception as e:
    # any other unexpected error will be propagated
    raise e
from datefinder import find_dates

string_with_dates = """
...
entries are due by January 4th, 2017 at 8:00pm
...
created 01/15/2005 by ACME Inc. and associates.
...
Liverpool NY 13088 42 cases
"""

matches = find_dates(string_with_dates)
print(type(matches))  # <class 'generator'>

while True:

    try:
        m = next(matches)

    # this is the exception seen by the program, rather than IllegalMonthError
    except TypeError as e:
        print(f"TypeError {e}")
        continue

    # the generator has no more items
    except StopIteration as e:
        print(f"StopIteration {e}")
        break

    # any other unexpected error will be propagated
    except Exception as e:
        raise e

    print(f"m {m}")
从编辑中更新

请注意,回溯显示

----> 9     for d in dates:
这里提出了免责条款。事实上,在查看文档时,您会看到
find_dates
返回一个生成器:

返回生成datetime.datetime对象的生成器,或 包含源文本和索引的元组(如果需要)

因此,在调用
find_dates
时,实际的日期解析不是完成的,而是在迭代结果时完成的。这使得在
try/catch
中进行包装变得更加困难,因为您必须逐项迭代生成器,每个项都在一个单独的
try/catch
块中:

try:
    datefinder.find_dates(b)
except IllegalMonthError as e:
    # this will print the error, but will not stop the program
    print(e)
except Exception as e:
    # any other unexpected error will be propagated
    raise e
from datefinder import find_dates

string_with_dates = """
...
entries are due by January 4th, 2017 at 8:00pm
...
created 01/15/2005 by ACME Inc. and associates.
...
Liverpool NY 13088 42 cases
"""

matches = find_dates(string_with_dates)
print(type(matches))  # <class 'generator'>

while True:

    try:
        m = next(matches)

    # this is the exception seen by the program, rather than IllegalMonthError
    except TypeError as e:
        print(f"TypeError {e}")
        continue

    # the generator has no more items
    except StopIteration as e:
        print(f"StopIteration {e}")
        break

    # any other unexpected error will be propagated
    except Exception as e:
        raise e

    print(f"m {m}")
从datefinder导入查找日期
带日期“”的字符串“”
...
报名截止时间为2017年1月4日晚上8:00
...
由ACME Inc.及其合伙人于2005年1月15日创建。
...
利物浦NY 13088 42例
"""
匹配=查找日期(带日期的字符串)
打印(键入(匹配项))#
尽管如此:
尝试:
m=下一个(匹配项)
#这是程序看到的例外,而不是非法的
除类型错误为e外:
打印(f“TypeError{e}”)
持续
#生成器没有其他项
除了作为e的StopIteration:
打印(f“StopIteration{e}”)
打破
#将传播任何其他意外错误
例外情况除外,如e:
提高e
打印(f“m{m}”)
您可以使用
m
执行任何需要的操作


干杯

使用
try/except
块:

try:
    datefinder.find_dates(b)
except IllegalMonthError as e:
    # this will print the error, but will not stop the program
    print(e)
except Exception as e:
    # any other unexpected error will be propagated
    raise e
from datefinder import find_dates

string_with_dates = """
...
entries are due by January 4th, 2017 at 8:00pm
...
created 01/15/2005 by ACME Inc. and associates.
...
Liverpool NY 13088 42 cases
"""

matches = find_dates(string_with_dates)
print(type(matches))  # <class 'generator'>

while True:

    try:
        m = next(matches)

    # this is the exception seen by the program, rather than IllegalMonthError
    except TypeError as e:
        print(f"TypeError {e}")
        continue

    # the generator has no more items
    except StopIteration as e:
        print(f"StopIteration {e}")
        break

    # any other unexpected error will be propagated
    except Exception as e:
        raise e

    print(f"m {m}")
从编辑中更新

请注意,回溯显示

----> 9     for d in dates:
这里提出了免责条款。事实上,在查看文档时,您会看到
find_dates
返回一个生成器:

返回生成datetime.datetime对象的生成器,或 包含源文本和索引的元组(如果需要)

因此,在调用
find_dates
时,实际的日期解析不是完成的,而是在迭代结果时完成的。这使得在
try/catch
中进行包装变得更加困难,因为您必须逐项迭代生成器,每个项都在一个单独的
try/catch
块中:

try:
    datefinder.find_dates(b)
except IllegalMonthError as e:
    # this will print the error, but will not stop the program
    print(e)
except Exception as e:
    # any other unexpected error will be propagated
    raise e
from datefinder import find_dates

string_with_dates = """
...
entries are due by January 4th, 2017 at 8:00pm
...
created 01/15/2005 by ACME Inc. and associates.
...
Liverpool NY 13088 42 cases
"""

matches = find_dates(string_with_dates)
print(type(matches))  # <class 'generator'>

while True:

    try:
        m = next(matches)

    # this is the exception seen by the program, rather than IllegalMonthError
    except TypeError as e:
        print(f"TypeError {e}")
        continue

    # the generator has no more items
    except StopIteration as e:
        print(f"StopIteration {e}")
        break

    # any other unexpected error will be propagated
    except Exception as e:
        raise e

    print(f"m {m}")
从datefinder导入查找日期
带日期“”的字符串“”
...
报名截止时间为2017年1月4日晚上8:00
...
由ACME Inc.及其合伙人于2005年1月15日创建。
...
利物浦NY 13088 42例
"""
匹配=查找日期(带日期的字符串)
打印(键入(匹配项))#
尽管如此:
尝试:
m=下一个(匹配项)
#这是程序看到的例外,而不是非法的
除类型错误为e外:
打印(f“TypeError{e}”)
持续
#生成器没有其他项
除了作为e的StopIteration:
打印(f“StopIteration{e}”)
打破
#将传播任何其他意外错误
例外情况除外,如e:
提高e
打印(f“m{m}”)
您可以使用
m
执行任何需要的操作


干杯

您可以添加一个
if
语句,检查给定月份是否在
1-12
范围内,如果