2

在进行一些数据清理时,我注意到dateutil.parser.parse未能拒绝某个格式错误的日期,认为其中的第一个数字是两位数的年份。这个图书馆可以被迫将两位数的年份视为无效吗?

例子:

from dateutil.parser import parse
parse('22-23 February')

输出:

datetime.datetime(2022, 2, 23, 0, 0)
4

1 回答 1

3

我设法通过参数传递一个自定义dateutil.parser.parserinfo对象来解决这个问题。幸运的是,有一个可以在派生类中重载的方法,以便对年份执行额外的验证。parserinfodateutil.parser.parsedateutil.parser.parserinfoconvertyear

from dateutil.parser import parse, parserinfo

class NoTwoDigitYearParserInfo(parserinfo):
    def convertyear(self, year, century_specified=False):
        if year < 100 and not century_specified:
            raise ValueError('Two digit years are not supported.')
        return parserinfo.convertyear(self, year, century_specified)

parse('22-23 February', parserinfo = NoTwoDigitYearParserInfo())

输出:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/site-packages/dateutil/parser.py", line 1162, in parse
    return parser(parserinfo).parse(timestr, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/dateutil/parser.py", line 552, in parse
    res, skipped_tokens = self._parse(timestr, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/dateutil/parser.py", line 1055, in _parse
    if not info.validate(res):
  File "/usr/local/lib/python3.5/site-packages/dateutil/parser.py", line 360, in validate
    res.year = self.convertyear(res.year, res.century_specified)
  File "<stdin>", line 4, in convertyear
ValueError: Two digit years are not supported.
于 2016-08-29T00:49:16.903 回答