Using datetime and managing date strings using python

I have a file in 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 will retrieve the “Summary” at the given “date” and “time”. For example, a function will have two arguments, a date and time, which will not be in date time formats. He should check if the date and time specified in the function argument matches between the date and time in DateStart and DateEnd in the file.

I am not sure how to get the time and date from the format indicated above [i.e. 20100629T110000]. I tried to use the following line_time = datetime.strptime(time, "%Y%D%MT%H%M%S")where the time is "20100629T110000", but I get a lot of errors, for example, "datetime.datetime does not have the strptime attribute".

What is the correct way to perform this function, thanks in advance.

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

Here is my mistake

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 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()
+3
source share
2 answers

Do not confuse a moduledatetime with datetimeObjects in a module .

The module has no function strptime, but the object has a class method 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)

Note the second time we should refer to the class as datetime.datetime.

Alternatively, you can import only 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)

In addition, I changed your line from %Y%D%MT%H%M%Sto %Y%m%dT%H%M%S, which I think you need.

+6
source

, Python. . strptime docs 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]))
+6

Source: https://habr.com/ru/post/1758876/


All Articles