Мне данная задача показалось достаточно интересной, чтобы потратить на нее некоторое время.
решение:
import re
import time
import pandas as pd
from datetime import datetime as DT
#библиотека функций для парсинга:
def tm_to_min(t, fmt='%H:%M %p'):
try:
t = time.strptime(t, '%I %p')
except ValueError:
t = time.strptime(t, '%I:%M %p')
# return # of minutes from the midnight
return t.tm_hour*60 + t.tm_min
def parse_time_range(s):
tm_from, tm_to = re.findall(r'(\d{1,2}\:?\d*?\s+[ap]m)', s)
return tm_to_min(tm_from), tm_to_min(tm_to)
def range_to_csv(s):
if re.match('\d$', s):
return s
m = re.search(r'(\d)\s*-\s*(\d)', s)
if m:
a,b = map(int, m.groups())
return ''.join(map(str, (range(a, b+1))))
else:
return ''
def range_to_list(s):
if re.match('\d$', s):
return [int(s)]
m = re.search(r'(\d)\s*-\s*(\d)', s)
if m:
a,b = map(int, m.groups())
return list(range(a, b+1))
else:
return []
def get_weekdays(s):
# cut off a time range
s = re.sub(r'\s+\d.*$', '', s)
s = (s.replace('Mon', '1')
.replace('Tue', '2')
.replace('Wed', '3')
.replace('Thu', '4')
.replace('Fri', '5')
.replace('Sat', '6')
.replace('Sun', '7')
)
ret = ''
for x in re.split('\s*,\s*', s):
#ret += range_to_list(x)
ret += range_to_csv(x)
return ret
def parse_sched(s):
weekdays = get_weekdays(s)
m_from, m_to = parse_time_range(s)
return pd.Series([weekdays, m_from, m_to])
def dt_to_sched(s):
d = pd.to_datetime(s)
w = str(d.week+1)
minutes = d.hour * 60 + d.minute
return w,minutes
def create_schedule(df):
# функция "explode()" отсюда: https://stackoverflow.com/a/40449726/5741205
t = explode(df.assign(sched=df.sched.str.split('\s*/\s*')), 'sched')
t[['weekdays','min_from','min_to']] = t.sched.apply(parse_sched)
# replace time ranges like "11 am - 12:30 am" --> "11:00 - 23:59:59"
t.loc[t.min_to < t.min_from, 'min_to'] = 24*60
return t
#############################################
# парсим CSV
df = pd.read_csv(r'C:\download\schedule.csv', header=None, names=['name', 'sched'])
# создаем расписание в виде нормализованного DF
t = create_schedule(df)
# имитируем ввод даты пользователем
user_date = 'Feb 02 2019 9PM'
w, mins = dt_to_sched(user_date)
# проверка расписания
res = t.loc[t.weekdays.str.contains(w) & (mins >= t.min_from) & (mins <= t.min_to), 'name'].drop_duplicates()
результат:
In [394]: res
Out[394]:
0 Kushi Tsuru
2 Osakaya Restaurant
4 The Stinking Rose
6 McCormick & Kuleto's
7 Mifune Restaurant
9 The Cheesecake Factory
11 New Delhi Indian Restaurant
14 Iroha Restaurant
Name: name, dtype: object
как выглядит DataFrame t
с расписанием:
In [395]: t
Out[395]:
name sched weekdays min_from min_to
0 Kushi Tsuru Mon-Sun 11:30 am - 9 pm 1234567 690 1260
1 Osakaya Restaurant Mon-Thu, Sun 11:30 am - 9 pm 12347 690 1260
2 Osakaya Restaurant Fri-Sat 11:30 am - 9:30 pm 56 690 1290
3 The Stinking Rose Mon-Thu, Sun 11:30 am - 10 pm 12347 690 1320
4 The Stinking Rose Fri-Sat 11:30 am - 11 pm 56 690 1380
5 McCormick & Kuleto's Mon-Thu, Sun 11:30 am - 10 pm 12347 690 1320
6 McCormick & Kuleto's Fri-Sat 11:30 am - 11 pm 56 690 1380
7 Mifune Restaurant Mon-Sun 11 am - 10 pm 1234567 660 1320
8 The Cheesecake Factory Mon-Thu 11 am - 11 pm 1234 660 1380
9 The Cheesecake Factory Fri-Sat 11 am - 12:30 am 56 660 1440 # <-- время после полуночи игнорируется
10 The Cheesecake Factory Sun 10 am - 11 pm 7 600 1380
11 New Delhi Indian Restaurant Mon-Sat 11:30 am - 10 pm 123456 690 1320
12 New Delhi Indian Restaurant Sun 5:30 pm - 10 pm 7 1050 1320
13 Iroha Restaurant Mon-Thu, Sun 11:30 am - 9:30 pm 12347 690 1290
14 Iroha Restaurant Fri-Sat 11:30 am - 10 pm 56 690 1320
11 am - 12:30 am
(11:00 - 00:30
) очень трудно будет обрабатывать