I have a list, matched_rows_2 which looks like this:
[[1 01-01-2021 10:18:00 100 TTF],
[2 01-01-2021 10:18:00 100 GGY],
[3 01-01-2021 10:22:00 120 HHJ],
[4 01-01-2021 10:23:00 160 JJH],
[5 01-01-2021 10:26:14 160 RRT],
[6 01-01-2021 10:27:59 160 PPO],
[7 01-01-2021 10:29:58 100 KKG],
[8 01-01-2021 10:30:50 160 PPO]]
Now for every x inside above list Im trying to get the x which has a datetime that is the closest to chosen_datetime.
This is my code:
chosen_datetime = 2021-01-01 10:16:00+00:00 #<--- this is a datetime aware time object.
timezone = "Europe/London"
chosen_datetime = pytz.utc.localize(chose_datetime).astimezone(pytz.timezone(timezone))
timeDiff = chosen_datetime.utcoffset().total_seconds()
chosen_datetime = pytz.utc.localize(chosen_datetime + datetime.timedelta(seconds=timeDiff)
matched_rows_3 = []
for x in matched_rows_2:
start_traject_time = x[1] + ' ' + x[2]
start_traject_time = datetime.datetime.strptime(start_traject_time, '%d-%m-%Y %H:%M:%S')
start_traject_time = pytz.utc.localize(start_traject_time)
if min(start_traject_time, key=lambda d: abs(d - chosen_datetime)):
matched_rows_3.append(x)
This the my current output:
if min(start_traject_time, key=lambda d: abs(d - chosen_datetime)):
TypeError: 'datetime.datetime' object is not iterable
This is my desired output:
1 01-01-2021 10:18:00 100 TTF
2 01-01-2021 10:18:00 100 GGY
3 01-01-2021 10:22:00 120 HHJ
4 01-01-2021 10:23:00 160 JJH
Please note that my datetime objects are all datetime aware objects, I want to keep it like that.
Aucun commentaire:
Enregistrer un commentaire