如何在python中使用日期范围来使用时间和日期来提取/查询数据

时间:2017-10-13 07:05:03

标签: python python-3.x python-3.6 python-datetime

下面是我的代码,它抓取数据并将数据转换为CSV文件(这是有效的)。我试图只关注从午夜到下午4点(BST(英国夏令时间)UTC / GMT +1小时)返回的数据,使用的日期有些如何。

有人可以告诉我这是怎么做的,DTDT是约会对象。

如果我想要实现的目标没有意义,请告诉我,我会尝试解释它。

我的代码:

from elasticsearch import Elasticsearch
import csv

es = Elasticsearch(["9200"])

# Replace the following Query with your own Elastic Search Query
res = es.search(index="search", body=
                {
                    "_source": ["DTDT", "TRDT", "SPLE", "RPLE"],
                    "query": {
                        "bool": {
                            "should": [
                                {"wildcard": {"CN": "TEST1"}}

                            ]
                        }
                    }
}, size=10)



header_names = { 'DTDT': 'DATE', 'TRDT': 'TIME', ...}

with open('mycsvfile.csv', 'w') as f:  # Just use 'w' mode in 3.x
    header_present  = False
    for doc in res['hits']['hits']:
        my_dict = doc['_source'] 
        if not header_present:
            w = csv.DictWriter(f, my_dict.keys())
            w.writerow(header_names)  # will write DATE, TIME, ... in correct place
            header_present = True


        w.writerow(my_dict)

例如,我想只从午夜到下午2点返回数据(使用当前日期)。

1 个答案:

答案 0 :(得分:0)

在将其写入csv文件之前,您可以检查时间范围,然后决定将其写入文件。

添加以下功能以检查时间范围:

def time_in_range(start, end, x):
    """Return true if x is in the range [start, end]"""
    if start <= end:
        return start <= x <= end
    else:
        return start <= x or x <= end

如果给定时间在范围内

,则为真

然后在您的代码中添加此内容。

import datetime
#Range here(Midnight to 2 PM)
start = datetime.time(0,0,0)
end = datetime.time(14,0,0)

with open('mycsvfile.csv', 'w') as f:  # Just use 'w' mode in 3.x
    header_present  = False
    for doc in res['hits']['hits']:
        my_dict = doc['_source']
        if not header_present:
            w = csv.DictWriter(f, my_dict.keys())
            w.writerow(header_names)  # will write DATE, TIME, ... in correct place
            header_present = True

        #Get time
        curr_time  = my_dict['DTDT']
        #Conver it into datetime object
        d_obj = datetime.datetime.strptime(curr_time, '%d/%m/%Y %H:%M')
        #Check whether it is in range using above function
        #If in range, then it will write to file
        if time_in_range(start, end, d_obj.time()):
            w.writerow(my_dict)