使用datetime和使用python

问题描述:

操作日期字符串,我有以下格式使用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

不要混淆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

您需要真正阅读适合您的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]))