'How to get the time from a text document and subtract the current time from it
I have a text file with the following structure: (city, arrival time, departure time )
New-York 14:40 8:00
Los-Angeles 12:00 17:00
San-Diego 14:15 7:10
I find the city I want, and then I need to get the arrival time and the departure time. I can't get the departure time
hours_arrival = re.compile(r''+city+'.*?([0-9]+)')
min_arrival = re.compile(r''+city+'.*?([0-9]+ +)')
hours_departure =
min_departure =
with open('base.txt') as f:
for line in f:
match =hours_arrival.match(line)
if match:
hs = int(match.group(1))
match = min_arrival.match(line)
if match:
ms = int(match.group(1))
Then subtract the arrival time from the current_time and compare it to <= 1 hour and get only the number of minutes. I get an error. I can't figure out how to subtract from the current time.
current_time = datetime.datetime.now().time()
t4 = timedelta(hours=hs, minutes=ms)
t5 = timedelta(hours=1, minutes=00)
min = t4 - current_time
if min <= t5:
print(min)
else:
print('no')
Solution 1:[1]
As I've mentioned in this comment it'll be better to use csv.reader() to read table-like file. It's not clear what divider used in this file. It could be either tab (\t) or multiple spaces. So to read file and split each row by columns you can use next code:
import csv
with open('base.txt') as f:
# if columns divided by '\t'
reader = csv.reader(f, delimiter='\t')
# if columns divided by spaces
reader = csv.reader(f, delimiter=' ', skipinitialspace=True)
for row in reader:
print(row)
To find current time you can use datetime.now() and to parse time string you can use datetime.strptime() with %H:%M format. Also you will need to set year, month and day to current date in parsed datetime object which can be done using datetime.replace(). Code:
from datetime import datetime
arrival = '12:00'
now = datetime.now()
arrival_time = datetime.strptime(arrival, '%H:%M').\
replace(now.year, now.month, now.day)
difference = now - arrival_time
if 0 <= difference.total_seconds() <= 3600:
print(difference)
else:
print('no')
So combining two code samples above you can get next code:
import csv
from datetime import datetime
with open('base.txt') as f:
reader = csv.reader(f, delimiter='\t')
for row in reader:
try:
arrival_time = datetime.strptime(row[1], '%H:%M')
departure_time = datetime.strptime(row[2], '%H:%M')
except (IndexError, ValueError):
pass
else:
now = datetime.now()
arrival_time = arrival_time.replace(now.year, now.month, now.day)
departure_time = departure_time.replace(now.year, now.month, now.day)
difference = now - arrival_time
if 0 <= difference.total_seconds() <= 3600:
print(difference)
else:
print('no')
Solution 2:[2]
Maybe like this: in arrival_time substitute hour and minute with time of arrival
current_time = datetime.now()
arrival_time = datetime(current_time.year, current_time.month, current_time.day, hour=8, minute=14)
mm = (current_time - arrival_time).seconds // 60
if mm <= 60:
print(mm)
else:
print('no')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
