views:

63

answers:

2

I have a file of the following format

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

I need to create a function that would retrieve "Summary" at a given "date" and "time". For example the function would have two arguments, the date and time, which wont be in date time formats. It needs to check if the date and time specified in the function argument is between the date and times in the DateStart and DateEnd in the file.

I am not sure how to retrieve time and date from the format specified above [i.e., 20100629T110000]. I was trying to use the following line_time = datetime.strptime(time, "%Y%D%MT%H%M%S"), where time is "20100629T110000", but i am getting a lot of errors, like " datetime.datetime has not attribute strptime".

Whats the right way to do this function, thanks in advance.

....................EDIT................

Here is my error

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'
>>> 

And here is my code

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  A: 

Don't confuse the datetime module with the datetime Objects in the module.

The module has no strptime function, but the Object does have a strptime class method:

>>> 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)

Note the second time we have to reference the class as datetime.datetime.

Alternatively you can import just the class:

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

Also, I changed your format string from %Y%D%MT%H%M%S to %Y%m%dT%H%M%S which I think is what you want.

Dave Webb
Thanks for the reply. I tried importing the class, i am still getting the same error. Also, once this is fixed, could line_time.year return 2010?
@user392409: You are getting the "datetime.datetime has not attribute strptime" error because you're still making the above mistake **somewhere**.
S.Lott
Once this is working `line_time.year` will be 2010; note it is an attribute so it *is* 2010 rather than a method *returns* 2010. What is the error message you're getting? If it's `'module' object has no attribute 'strptime'` then you haven't imported the class.
Dave Webb
have edited my post with the code i am running and also the error i am getting. Thanks
Thanks for the comments, I guess I was reading the documentation for Python 2.7 and running on 2.4!
+2  A: 

You need to actually read the documentation appropriate for your version of Python. See the note on strptime in the docs for datetime:

New in version 2.5.

and you are using version 2.4. You will need to use the workaround mentioned in that documentation:

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