2013-02-14 44 views
1

我想分割日期時間...它很好地存儲日期,但每當我嘗試和存儲時間,我得到一個錯誤。問題分裂日期時間 - 'str'對象沒有屬性'strptime'

下面的代碼工作:

datetime = tweet.date.encode('ascii', 'ignore') 
struct_date = time.strptime(datetime, "%a, %d %b %Y %H:%M:%S +0000") 
date = time.strftime("%m/%d/%Y") 

但是,如果我添加以下行,我得到一個錯誤:

time = time.strftime("%H:%M:%S") 

AttributeError的: '海峽' 對象有沒有屬性 'strptime'

回答

5

您爲一個名爲time的變量分配了一個字符串。使用不同的名稱,它掩蓋了您的time模塊導入。

tm = time.strptime(datetime, "%H:%M:%S") 
2

它可能工作過一次,然後停止工作,因爲你用一個名爲'time'的變量覆蓋了模塊的'time'。使用不同的變量名稱。

這將覆蓋時間模塊

>>> import time 
>>> type(time) 
<type 'module'> 
>>> time = time.strftime("%H:%M:%S") 
>>> type(time) 
<type 'str'> 
>>> time = time.strftime("%H:%M:%S") 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
AttributeError: 'str' object has no attribute 'strftime' 

這是你應該怎麼做

>>> import time 
>>> type(time) 
<type 'module'> 
>>> mytime = time.strftime("%H:%M:%S") 
>>> type(time) 
<type 'module'> 
>>> time.strftime("%H:%M:%S") 
'11:05:08' 
相關問題