获取两个日期之间的元素

时间:2013-11-21 11:55:25

标签: python date

如下生成的属性

['2013-05-10T13:07:19.425602+01:00', 'seta', 'zone1', 'status=Info', 'id=2', 'MSG="New', 'Request', 'Arrival"','Reqno=103']
['2013-05-10T13:07:19.586633+01:00', 'seta', 'zone1', 'status=Info', 'id=21', 'MSG="New', 'Request', 'Arrival"','Reqno=1021']
['2013-05-20T13:07:21.842280+01:00', 'seta', 'zone1', 'status=Info', 'id=28', 'MSG="New', 'Request', 'Arrival"','Reqno=1028']
['2013-05-21T13:07:21.853704+01:00', setc', 'zone14', 'status=Info', 'id=29', 'MSG="New', 'Request', 'Arrival"','Reqno=1897']
['2013-05-21T13:07:19.425602+01:00', 'setc', 'zone14', 'status=Info', 'id=211', 'MSG="New', 'Request', 'Arrival"','Reqno=2020']
['2013-05-22T13:07:19.586633+01:00', 'setc', 'zone14', 'status=Info', 'id=212', 'MSG="New', 'Request', 'Arrival"','Reqno=2021']
['2013-05-22T13:07:21.842280+01:00', 'setc', 'zone14', 'status=Info', 'id=218', 'MSG="New', 'Request', 'Arrival"','Reqno=2023']
['2013-05-22T13:07:21.853704+01:00', seta', 'zone1', 'status=Info', 'id=291', 'MSG="New', 'Request', 'Arrival"','Reqno=2024']

我需要获取两个日期之间的属性2013-05-21 13:07:192013-05-22 13:07:19

for line in pass:
    if string.find(line, searchterm) != -1:
    attributes = line.split()   
        start = attributes[0]
    start = start[:-13] 
    start = strptime(start, '%Y-%m-%dT%H:%M:%S')
    #print start
    untill = start+timedelta(days=1)
    #print untill


    #delta = start - untill
    for i in attributes:
      month, day, year hours:minutes:seconds = i[0].split('-')
      r_date = date(year, month, day hours:minutes:seconds)
      if r_date >= start_date and r_date <= end_date:
         return attributes.append(i)

任何人都可以指导我吗?如何获取两个日期之间的属性?

预期产出:

['2013-05-21 13:07:21', setc', 'zone14', 'status=Info', 'id=29', 'MSG="New', 'Request', 'Arrival"','Reqno=1897']
['2013-05-21 13:07:19', 'setc', 'zone14', 'status=Info', 'id=211', 'MSG="New', 'Request', 'Arrival"','Reqno=2020']
['2013-05-22 13:07:19', 'setc', 'zone14', 'status=Info', 'id=212', 'MSG="New', 'Request', 'Arrival"','Reqno=2021']
['2013-05-22 13:07:21', 'setc', 'zone14', 'status=Info', 'id=218', 'MSG="New', 'Request', 'Arrival"','Reqno=2023']
['2013-05-22 13:07:21', seta', 'zone1', 'status=Info', 'id=291', 'MSG="New', 'Request', 'Arrival"','Reqno=2024']

如何将开始日期和结束日期指定为usr输入?

修改位:

startdate = raw_input("Enter start date as YYYY-MM-DD HH:MM:SS :") 
enddate = raw_input ("Enter end date as  YYYY-MM-DD HH:MM:SS :") 
            try: 
                   start = datetime.datetime.strptime(startdate, '%Y-%m-%d %H:%M:%S')  
            end = datetime.datetime.strptime(enddate, '%Y-%m-%d %H:%M:%S')

            except ValueError:
            print "Invalid date"                                


            for item in attributes:

           _date = datetime.datetime(time.strptime(item[0], '%Y-%m-%dT%H:%M:%S'))
        on compilation at this section it gives an error time data 2 doesnot match the format  '%Y-%m-%dT%H:%M:%S' why is considering only the first digit of the date ?

1 个答案:

答案 0 :(得分:3)

如果使用datetime个对象,则更容易进行此检查。

首先将您的开始和结束日期定义为datetime对象

start = datetime(2013, 5, 21, 13, 7, 19) 
end = datetime(2013, 5, 22, 13, 7, 19) 

假设您的数据位于my_values列表中

my_values = [
    ['2013-05-10T13:07:19.425602+01:00', 'seta', 'zone1', 'status=Info', 'id=2', 'MSG="New', 'Request', 'Arrival"','Reqno=103']
    ['2013-05-10T13:07:19.586633+01:00', 'seta', 'zone1', 'status=Info', 'id=21', 'MSG="New', 'Request', 'Arrival"','Reqno=1021']
    ['2013-05-20T13:07:21.842280+01:00', 'seta', 'zone1', 'status=Info', 'id=28', 'MSG="New', 'Request', 'Arrival"','Reqno=1028']
    ['2013-05-21T13:07:21.853704+01:00', setc', 'zone14', 'status=Info', 'id=29', 'MSG="New', 'Request', 'Arrival"','Reqno=1897']
    ['2013-05-21T13:07:19.425602+01:00', 'setc', 'zone14', 'status=Info', 'id=211', 'MSG="New', 'Request', 'Arrival"','Reqno=2020']
    ['2013-05-22T13:07:19.586633+01:00', 'setc', 'zone14', 'status=Info', 'id=212', 'MSG="New', 'Request', 'Arrival"','Reqno=2021']
    ['2013-05-22T13:07:21.842280+01:00', 'setc', 'zone14', 'status=Info', 'id=218', 'MSG="New', 'Request', 'Arrival"','Reqno=2023']
    ['2013-05-22T13:07:21.853704+01:00', seta', 'zone1', 'status=Info', 'id=291', 'MSG="New', 'Request', 'Arrival"','Reqno=2024']
]

创建新列表:

my_selected_values = []

循环浏览当前列表并选择所需日期

for item in my_values:
    _date = datetime.strptime(item[0], '%Y-%m-%dT%H:%M:%S')  # parse datetime from string
    if start <= _date <= end:  # check if it is within your time interval
        my_selected_values.append([_date.strftime('%Y-%m-%d %H:%M:%S')] + item[1:])

如果item日期时间在您的范围内,则您将相关日期时间值重新格式化为您选择的字符串,并添加其余值。 [_date.strftime('%Y-%m-%d %H:%M:%S')]可让您根据需要设置日期时间数据的格式,+ item[1:]将添加原始列表中的其余项目并根据需要创建输出。最后你将return my_selected_values

更新:关于从用户获取开始和结束时间,您可以使用raw_input(python 2.x)或input(python 3.x),然后将输入字符串解析为datetime对象。像:

_start_date = raw_input("Enter start date as YYYY-MM-DD HH:MM:SS")  # You can specify input format as you wish since you will parse the string manually
try: 
    start = datetime.strftime(_start_date, '%Y-%m-%d %H:%M:%S')  # use your datetime input format you specify above
except ValueError:
    print "Invalid date input"

或者,如果您愿意,您可以逐个获取日期时间参数(年,月,日等...),然后使用每个输入创建日期时间对象

_year = int(raw_input("Year"))
_month = int(raw_input("month"))
etc...
start = datetime(_year, _month, ...)