2010-08-10 45 views
3

操作日期字符串,我有以下格式使用datetime和使用python

Summary:meeting Description:None DateStart:20100629T110000 DateEnd:20100629T120000 Time:20100805T084547Z 
Summary:meeting Description:None DateStart:20100630T090000 DateEnd:20100630T100000 Time:20100805T084547Z 

我需要創建一個將檢索「綱要」在給定的「日期」和「時間」功能的文件。 例如,函數將有兩個參數,日期和時間,它不會是日期時間格式。它需要檢查函數參數中指定的日期和時間是否在文件的DateStart和DateEnd中的日期和時間之間。

我不確定如何從上面指定的格式[即20100629T110000]中檢索時間和日期。我試圖使用以下 line_time = datetime.strptime(time, "%Y%D%MT%H%M%S"),其中時間是「20100629T110000」,但我收到了很多錯誤,如「datetime.datetime沒有屬性strptime」。

什麼是正確的方法來做到這一點,在此先感謝。

....................編輯................

這裏是我的錯誤

Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)] on win32 
Type "copyright", "credits" or "license()" for more information. 

    **************************************************************** 
    Personal firewall software may warn about the connection IDLE 
    makes to its subprocess using this computer's internal loopback 
    interface. This connection is not visible on any external 
    interface and no data is sent to or received from the Internet. 
    **************************************************************** 

>>> 
Traceback (most recent call last): 
    File "C:\Python24\returnCalendarstatus", line 24, in -toplevel- 
    status = calendarstatus() 
    File "C:\Python24\returnCalendarstatus", line 16, in calendarstatus 
    line_time = datetime.strptime(time, "%Y%m%dT%H%M%S") 
AttributeError: type object 'datetime.datetime' has no attribute 'strptime' 
>>> 

這裏是我的代碼

import os 
import datetime 
import time 
from datetime import datetime 

def calendarstatus(): 

    g = open('calendaroutput.txt','r') 
    lines = g.readlines() 
    for line in lines:   
     line=line.strip() 
     info=line.split(";") 
     summary=info[1] 
     description=info[2] 
     time=info[5]; 
     line_time = datetime.strptime(time, "%Y%m%dT%H%M%S") 
     return line_time.year 

status = calendarstatus() 
+1

請顯示確切的錯誤消息。 – 2010-08-10 09:42:02

回答

6

不要混淆the datetime Objects in the modulethe datetime module

的模塊沒有strptime功能,但對象確實有strptime類方法:

>>> time = "20100629T110000" 
>>> import datetime 
>>> line_time = datetime.strptime(time, "%Y%m%dT%H%M%S") 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
AttributeError: 'module' object has no attribute 'strptime' 
>>> line_time = datetime.datetime.strptime(time, "%Y%m%dT%H%M%S") 
>>> line_time 
datetime.datetime(2010, 6, 29, 11, 0) 

注意,我們必須引用類爲datetime.datetime第二次。

另外,您可以只導入類:

>>> from datetime import datetime 
>>> line_time = datetime.strptime(time, "%Y%m%dT%H%M%S") 
>>> line_time 
datetime.datetime(2010, 6, 29, 11, 0) 

另外,我改變了你format string%Y%D%MT%H%M%S%Y%m%dT%H%M%S我認爲這是你想要的。

+0

感謝您的回覆。我嘗試導入類,我仍然得到相同的錯誤。 此外,一旦這是固定的,line_time.year可以返回2010嗎? – user392409 2010-08-10 10:07:22

+0

@ user392409:您正在收到「datetime.datetime沒有屬性strptime」錯誤,因爲您仍然在上述錯誤**某處**。 – 2010-08-10 10:21:17

+0

一旦這個工作'line_time.year'將是2010;注意它是一個屬性,所以它*是* 2010而不是方法*返回* 2010。你得到的錯誤信息是什麼?如果它的「模塊」對象沒有屬性「strptime」,那麼你還沒有導入類。 – 2010-08-10 10:25:30

6

您需要真正閱讀適合您的Python版本的文檔。請參閱strptime中的注意事項docs for datetime

版本2.5中的新功能。

並且您使用的是版本2.4。您需要使用該文檔中提到的解決方法:

import time 
import datetime 
[...] 
time_string = info[5] 
line_time = datetime(*(time.strptime(time_string, "%Y%m%dT%H%M%S")[0:6]))