2

一些非常简单的代码会导致一个神秘的错误,而谷歌什么也没给我……

代码:

import sys
import datetime

for line in sys.stdin:
    date = datetime.datetime.strptime(line, '%Y%m%dT%H%M%S')
    print date

标准输入的前几行输入:

20101119T141500
20101119T164500
20110310T081500
20110310T113000
20100218T113000
...

错误:

Traceback (most recent call last):
  File "time.py", line 5, in <module>
    date = datetime.datetime.strptime(line, '%Y%m%dT%H%M%S')
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_strptime.py", line 13, in <module>
    import time
  File "<path>", line 5, in <module>
    date = datetime.datetime.strptime(line, '%Y%m%dT%H%M%S')
AttributeError: _strptime

平台:Mac OS X 10.7.3,Python 2.7.1

4

1 回答 1

13

您将脚本称为“time.py”,从而隐藏了time模块。不要那样做。

于 2012-04-27T19:36:01.390 回答