3
我想將字符串轉換爲日期。如果我正確地讀doc's,下面的代碼應該工作:Python:strptime()不工作
import datetime
d = datetime.strptime('January 01, 2015', '%B %d, %Y')
相反,我得到的錯誤:
AttributeError: 'module' object has no attribute 'strptime'
我想將字符串轉換爲日期。如果我正確地讀doc's,下面的代碼應該工作:Python:strptime()不工作
import datetime
d = datetime.strptime('January 01, 2015', '%B %d, %Y')
相反,我得到的錯誤:
AttributeError: 'module' object has no attribute 'strptime'
你需要從datetime
進口datetime
:
from datetime import datetime
d = datetime.strptime('January 01, 2015', '%B %d, %Y')
或使用:
import datetime
datetime.datetime.strptime('January 01, 2015', '%B %d, %Y')