简体   繁体   English

使用dateutil.parser.parse将ValueError抛出两位数的年份日期

[英]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.

相关问题 为什么dateutil.parser.parse()在此字符串上输入错误的年份? - Why does dateutil.parser.parse() get the year wrong on this string? dateutil.parser.parse()和丢失的时区信息 - dateutil.parser.parse() and lost timezone information 距离字符串的dateutil.parser.parse()? - The dateutil.parser.parse() of distance strings? 使用“import dateutil”和“dateutil.parser.parse()”时的AttributeError,但在使用“from dateutil import parser”时没有问题 - AttributeError when using “import dateutil” and “dateutil.parser.parse()” but no problems when using “from dateutil import parser” python标准lib相当于dateutil.parser.parse - python standard lib equivalent to dateutil.parser.parse 从 Python 中的 dateutil.parser.parse 获取时区 - Getting timezone from dateutil.parser.parse in Python Python dateutil.parser.parse首先解析月份,而不解析日期 - Python dateutil.parser.parse parses month first, not day 修改 dateutil.parser.parse 参数以纠正日期错误识别 - Modify dateutil.parser.parse parameters to correct date misidentification dateutil.parser.parse 不返回正确的日期时间 - dateutil.parser.parse does not return a correct datetime dateutil.parser.parse正在解析&#39;0001&#39;为2001.如何解决这个问题只能将其读作0001 - dateutil.parser.parse is parsing '0001' as 2001. How can i solve this to read it as 0001 only
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM