2015-06-16 20 views
3

我想將字符串轉換爲日期。如果我正確地讀doc's,下面的代碼應該工作:Python:strptime()不工作

import datetime 
d = datetime.strptime('January 01, 2015', '%B %d, %Y') 

相反,我得到的錯誤:

AttributeError: 'module' object has no attribute 'strptime' 

回答

6

你需要從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')