尝试将生成器转换为列表时发生OverflowError

时间:2019-01-31 16:26:26

标签: python python-3.x list date generator

我正在尝试使用datefinder.find_dates从txt文件中提取日期,该日期返回生成器对象。在出现以下错误之前,一切正常,直到我尝试将生成器转换为列表。

我一直在寻找解决方案,但是我无法找到解决方案,不确定我也不能真正理解问题。

import datefinder 
import glob

path = "some_path/*.txt"
files = glob.glob(path)  

dates_dict = {} 

for name in files:
   with open(name, encoding='utf8') as f:
       dates_dict[name] = list(datefinder.find_dates(f.read()))

返回:

---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
<ipython-input-53-a4b508b01fe8> in <module>()
  1 for name in files:
  2     with open(name, encoding='utf8') as f:
----> 3         dates_dict[name] = list(datefinder.find_dates(f.read()))

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

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

C:\ProgramData\Anaconda3\lib\site-packages\dateutil\parser\_parser.py in 
parse(timestr, parserinfo, **kwargs)
   1354         return parser(parserinfo).parse(timestr, **kwargs)
   1355     else:
-> 1356         return DEFAULTPARSER.parse(timestr, **kwargs)
   1357 
   1358 

C:\ProgramData\Anaconda3\lib\site-packages\dateutil\parser\_parser.py in 
parse(self, timestr, default, ignoretz, tzinfos, **kwargs)
    651             raise ValueError("String does not contain a date:", 
timestr)
    652 
--> 653         ret = self._build_naive(res, default)
    654 
    655         if not ignoretz:

C:\ProgramData\Anaconda3\lib\site-packages\dateutil\parser\_parser.py in 
_build_naive(self, res, default)
   1222             cday = default.day if res.day is None else res.day
   1223 
-> 1224             if cday > monthrange(cyear, cmonth)[1]:
   1225                 repl['day'] = monthrange(cyear, cmonth)[1]
   1226 

C:\ProgramData\Anaconda3\lib\calendar.py in monthrange(year, month)
    122     if not 1 <= month <= 12:
    123         raise IllegalMonthError(month)
--> 124     day1 = weekday(year, month, 1)
    125     ndays = mdays[month] + (month == February and isleap(year))
    126     return day1, ndays

C:\ProgramData\Anaconda3\lib\calendar.py in weekday(year, month, day)
    114     """Return weekday (0-6 ~ Mon-Sun) for year (1970-...), month(1- 12),
    115        day (1-31)."""
--> 116     return datetime.date(year, month, day).weekday()
    117 
    118 

OverflowError: Python int too large to convert to C long

有人可以清楚地解释吗?

预先感谢

重新编辑:在考虑了所做的评论之后,我找到了一个最小的,可读的和可验证的示例。错误发生在:

import datefinder

generator = datefinder.find_dates("466990103060049")

for s in generator:
   pass

1 个答案:

答案 0 :(得分:1)

这似乎是您正在使用的库中的错误。它试图将字符串解析为一年,但是今年太大了,Python无法处理。 string_agg()正在使用的库说在这种情况下它会引发一个datefinder,但是OverflowError却忽略了这种可能性。

一个快速而肮脏的骇客只是为了使它起作用:

datefinder