如何在python中使用日期范围来使用时间和日期来提取/查询数据
问题描述:
以下是我的代码,它抓取数据并将数据转换为CSV文件(这是可行的)。我试图只关注从午夜到下午4点(英国夏令时UTC/GMT +1小时)返回的数据,使用日期一些方法。如何在python中使用日期范围来使用时间和日期来提取/查询数据
有人可以告诉我这是怎么做的,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时(使用当前日期)。
答
在写入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)
请问您能分享一行包含数据的csv文件吗? –
@ Dinesh,这些是CSV中的示例行。 13/10/2017 00:00 \t F#b422560 \t 2017年7月9日16时55 \t F#b422562 \t 2017年5月9日6点24 \t F#b422576 \t 2017年5月9日6点24 \t F#b422578 \t 25/08/2017 12时26 \t F#b422505 \t 13/10/2017 13时24分\t个B#r110576 \t 2017年8月9日二点53 \t B#r110585 \t 13/10/2017 14:00 \t B#r110594 \t 例如,文件时正在创建我想从午夜到下午6点的当前日期数据,因为您可以看到有3行包含今天的数据,我只希望在文件 – Rich
@Dinesh中显示,想要进行聊天讨论。当我粘贴行时,格式在评论框中出错。不知道你是否可以正确看到它 – Rich