How to use date range in python to pull /query data using current date

我与影子孤独终老i 提交于 2019-12-13 03:14:01

问题


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

来源:https://stackoverflow.com/questions/46712620/how-to-use-date-range-in-python-to-pull-query-data-using-current-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!