我正在尝试运行一个非常基本的脚本:
import dateparser
dateparser.parse('12/12/12')
但我一直收到同样的错误
Traceback (most recent call last):
File ".../app/test.py", line 3, in <module>
dateparser.parse('12/12/12')
File ".../lib/python3.9/site-packages/dateparser/conf.py", line 92, in wrapper
return f(*args, **kwargs)
File ".../lib/python3.9/site-packages/dateparser/__init__.py", line 61, in parse
data = parser.get_date_data(date_string, date_formats)
File ".../lib/python3.9/site-packages/dateparser/date.py", line 428, in get_date_data
parsed_date = _DateLocaleParser.parse(
File ".../lib/python3.9/site-packages/dateparser/date.py", line 178, in parse
return instance._parse()
File ".../lib/python3.9/site-packages/dateparser/date.py", line 182, in _parse
date_data = self._parsers[parser_name]()
File ".../lib/python3.9/site-packages/dateparser/date.py", line 196, in _try_freshness_parser
return freshness_date_parser.get_date_data(self._get_translated_date(), self._settings)
File ".../lib/python3.9/site-packages/dateparser/date.py", line 234, in _get_translated_date
self._translated_date = self.locale.translate(
File ".../lib/python3.9/site-packages/dateparser/languages/locale.py", line 131, in translate
relative_translations = self._get_relative_translations(settings=settings)
File ".../lib/python3.9/site-packages/dateparser/languages/locale.py", line 158, in _get_relative_translations
self._generate_relative_translations(normalize=True))
File ".../lib/python3.9/site-packages/dateparser/languages/locale.py", line 172, in _generate_relative_translations
pattern = DIGIT_GROUP_PATTERN.sub(r'?P<n>d+', pattern)
File ".../lib/python3.9/site-packages/regex/regex.py", line 700, in _compile_replacement_helper
is_group, items = _compile_replacement(source, pattern, is_unicode)
File ".../lib/python3.9/site-packages/regex/_regex_core.py", line 1736, in _compile_replacement
raise error("bad escape \%s" % ch, source.string, source.pos)
regex._regex_core.error: bad escape d at position 7
它可能是日期解析器和python之间的兼容版本吗?我使用的是dateparser1.1和python 3.9,dateparsers的文档表明它接受这个python versoin。
我们的团队刚刚遇到了这个问题,导致我们的生产服务器瘫痪。昨晚,dateparser
、regex
的一个依赖项被更新为版本2022.3.15
。Dateparser安装regex,但没有冻结版本。最新版本的regex似乎不喜欢dateparser正在做的事情,这导致了您看到的错误。通过pip在日期解析器之前安装regex==2022.3.2
,这完全解决了我们的问题。
如果没有dateutil
,我不知道该怎么过!
试试这个:
python -m pip install python-dateutil
或conda锻造或其他什么(它不在标准库中(,然后:
>>> from dateutil.parser import parse as date_parse
>>> date_parse('12/12/12')
datetime.datetime(2012, 12, 12, 0, 0)
如果您有pandas
,则dateutil
已作为依赖项安装。
由于您提供了一种尴尬的格式,我会考虑为其设置格式
dateparser.parse('12/12/1212', date_formats=['%m/%d/%Y'])