Python dateutil:AttributeError:模块“dateutil”没有属性“parse”。

15

尝试使用dateutil解析未知格式的日期,但找不到任何文档方法?

代码:

import dateutil
print(dateutil.parser.parse("24.05.2017"))
exit(1)

错误:

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'
2个回答

18

您可以稍微修改您的代码来解决这个错误:

from dateutil import parser
print(parser.parse("24.05.2017"))

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


2
@Gunther,你的方法适用于Python3,能否解释一下? - GoTop

11
作为替代方案,如果你更改导入的方式,点号表示法仍然有效:
import dateutil.parser
print(dateutil.parser.parse("24.05.2017"))

谢谢。这个方法可行,而被采纳的答案则不行。 - n00b programmer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接