[英]Throw ValueError for two digit year dates with dateutil.parser.parse
While doing some data cleaning, I noticed that dateutil.parser.parse
failed to reject a certain malformed date, thinking that the first number in it is a two digit year. 在进行一些数据清理时,我注意到dateutil.parser.parse
无法拒绝某个格式错误的日期,并认为其中的第一个数字是两位数的年份。 Can this library be forced to treat two digit years as invalid? 可以强制将此库视为两位数的年份无效吗?
Example: 例:
from dateutil.parser import parse
parse('22-23 February')
outputs: 输出:
datetime.datetime(2022, 2, 23, 0, 0)
I managed to work around this by passing a custom dateutil.parser.parserinfo
object via the parserinfo
parameter to dateutil.parser.parse
. 我设法通过将自定义dateutil.parser.parserinfo
对象通过parserinfo
参数传递给dateutil.parser.parserinfo
来解决此dateutil.parser.parse
。 Luckily, dateutil.parser.parserinfo
has a convertyear
method that can be overloaded in a derived class in order to perform extra validations on the year. 幸运的是, dateutil.parser.parserinfo
有一个convertyear
方法,可以在派生类中重载convertyear
方法,以便对该年份执行额外的验证。
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())
outputs: 输出:
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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.