How to use date range in python to pull /query dat

2019-08-18 01:46发布

问题:

Below is my code that is grabbing data and converting the data into a CSV file (this is working). I am trying to only focus on the data that is returned from midnight till 4pm (BST (British Summer Time) UTC/GMT +1 hour) using the dates some how.

Could someone show me how this is done please, DTDT is the date.

If what I am trying to achieve does not make sense, just let me know I will try to explain it.

My Code:

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)

For example, I want to only return the data from midnight till 2pm (using the current date).