Python dateutil: AttributeError: module 'dateutil' has no attribute 'parse'

Cor*_*bin 8 parsing date python-3.x

Trying to use dateutil to parse dates from an unknown format but none of the documented methods are found?

CODE:

import dateutil
print(dateutil.parser.parse("24.05.2017"))
exit(1)
Run Code Online (Sandbox Code Playgroud)

ERROR:

Traceback (most recent call last):
  File "test.py", line 2, in <module>
    print(dateutil.parser.parse("24.05.2017"))
AttributeError: module 'dateutil' has no attribute 'parser'
Run Code Online (Sandbox Code Playgroud)

Gun*_*her 11

您可以通过稍微修改代码来修复此错误:

from dateutil import parser
print(parser.parse("24.05.2017"))
Run Code Online (Sandbox Code Playgroud)

由于 dateutil 包在内部组织其模块的方式,因此需要进行此更改。

  • @Gunther,你的方法适用于python3,你能解释一下吗? (2认同)

ted*_*r42 11

作为已接受答案的替代方案,如果您更改导入,点分符号仍然有效:

import dateutil.parser
print(dateutil.parser.parse("24.05.2017"))
Run Code Online (Sandbox Code Playgroud)