2012-12-11 74 views
3

我試圖將Twitter的「created_at」標記信息從XML文件轉換爲Python中的日期對象。我將一些代碼拼湊在一起,這些代碼使我獲得最多的途徑,但是當我嘗試將我找到的日期與其他日期對象進行比較時,則會崩潰。這是我到目前爲止有:將Twitter XML日期轉換爲Python日期對象

import time 
from datetime import datetime 

#Twitter part removed... generates a list of dates from the XML called date_list 

#Takes the first item from the list (date_list) and converts it to a string 
date_str = str(date_list[0]) 

#Takes the string (date_str) and converts it to datetime 
time_struct = time.strptime(date_str, "%a %b %d %H:%M:%S +0000 %Y") 
date_datetime = datetime.fromtimestamp(time.mktime(time_struct)) 

#Converts datetime to just date 
date = date_datetime.date() 

if date_datetime < datetime.now(): 
    print "yes" 

if date < datetime.date.today(): 
    print "yes, also" 

至於輸出,我得到一個肯定的,那麼「AttributeError的:‘method_descriptor’對象有沒有屬性‘今天’」最後一行。

我試着改變導入爲「導入日期時間」,但後來我得到以下錯誤AttributeError: 'module' object has no attribute 'fromtimestamp'並沒有輸出。

它似乎或者我導入日期時間和代碼fromtimestamp部分停止工作或我導入「從日期時間導入日期時間」,我不能創建日期對象?我已經看到了其他線程可以幫助你從Twitter日期到日期時間,但是你如何獲得所有的日期(無分鐘,秒鐘等)?

+1

你也有一條'import datetime',我敢打賭。 –

回答

0

嘗試使用datetime.now().date()datetime.today().date()。否則,你還需要from datetime import datedate.today()

+0

謝謝!這兩個工作。非常感激。 – user1895076

相關問題