从 .txt 文件中读取时间,然后倒计时到那个时间
Read time(s) from .txt file, then countdown to that time
我有一个名为 sorted_passes.txt 的文本文件,其中包含以下内容:
- NOAA18 23/08/2020 10:56:46 最高海拔:67
- NOAA19 23/08/2020 19:08:02 最高海拔:74
- NOAA15 23/08/2020 20:12:44 最高海拔:87
- NOAA18 2020 年 8 月 23 日 22:19:47 最高海拔:90
我想要一个计时器,或者执行以下操作之一:
- 倒计时到 .txt 文件中的下一个时间,然后在那个时间之后,移动到下一个时间,再次倒计时。
- 倒数到 .txt 文件中的每个时间
我的计划是最终通过连接到 raspberry pi 的 MAX7219 LED 板显示倒数计时器。
到目前为止,我有这个 python 代码:
# calculate time until next pass
from datetime import datetime
futuredate = datetime.strptime('10:56:46', '%H:%M:%S')
nowdate = datetime.now()
count = int((futuredate-nowdate).total_seconds())
days = count//86400
hours = (count-days*86400)//3600
minutes = (count-days*86400-hours*3600)//60
seconds = count-days*86400-hours*3600-minutes*60
print("Next Pass: {}h:{}m:{}s".format(hours, minutes, seconds))
这应该让你开始:
from datetime import datetime
from time import sleep
def compare(event):
"""Return True if it's counting down, false if the time already passed"""
now = datetime.now()
if now <= event:
diff = event - now
print("Countdown: {}".format(diff))
return True
else:
return False
def extract_timestamp(line):
"""Extract datetime from string:
NOAA18 23/08/2020 10:56:46 Max Elev: 67
"""
time_stamp = line[7:][:-14]
time_event = datetime.strptime(time_stamp, '%d/%m/%Y %H:%M:%S')
return time_event
def open_file():
with open('sorted_passes.txt', 'r') as f:
return f.readlines()
data = open_file()
# iterate through the lines of the file
for line in data:
ts = extract_timestamp(line)
while compare(ts):
sleep(1)
else:
print("Next event")
continue
print("Finished")
这将打印一个倒计时语句,说明需要多长时间,请稍等。否则它将转到下一个事件,直到检查所有行。
您需要确保文件日期是递增的(例如,新行总是晚于前面的行)。
示例输出(我手动更改了最后一行中的日期):
Next event
Next event
Next event
Countdown: 0:00:03.531014
Countdown: 0:00:02.526724
Countdown: 0:00:01.524277
Countdown: 0:00:00.518995
Finished
我有一个名为 sorted_passes.txt 的文本文件,其中包含以下内容:
- NOAA18 23/08/2020 10:56:46 最高海拔:67
- NOAA19 23/08/2020 19:08:02 最高海拔:74
- NOAA15 23/08/2020 20:12:44 最高海拔:87
- NOAA18 2020 年 8 月 23 日 22:19:47 最高海拔:90
我想要一个计时器,或者执行以下操作之一:
- 倒计时到 .txt 文件中的下一个时间,然后在那个时间之后,移动到下一个时间,再次倒计时。
- 倒数到 .txt 文件中的每个时间
我的计划是最终通过连接到 raspberry pi 的 MAX7219 LED 板显示倒数计时器。
到目前为止,我有这个 python 代码:
# calculate time until next pass
from datetime import datetime
futuredate = datetime.strptime('10:56:46', '%H:%M:%S')
nowdate = datetime.now()
count = int((futuredate-nowdate).total_seconds())
days = count//86400
hours = (count-days*86400)//3600
minutes = (count-days*86400-hours*3600)//60
seconds = count-days*86400-hours*3600-minutes*60
print("Next Pass: {}h:{}m:{}s".format(hours, minutes, seconds))
这应该让你开始:
from datetime import datetime
from time import sleep
def compare(event):
"""Return True if it's counting down, false if the time already passed"""
now = datetime.now()
if now <= event:
diff = event - now
print("Countdown: {}".format(diff))
return True
else:
return False
def extract_timestamp(line):
"""Extract datetime from string:
NOAA18 23/08/2020 10:56:46 Max Elev: 67
"""
time_stamp = line[7:][:-14]
time_event = datetime.strptime(time_stamp, '%d/%m/%Y %H:%M:%S')
return time_event
def open_file():
with open('sorted_passes.txt', 'r') as f:
return f.readlines()
data = open_file()
# iterate through the lines of the file
for line in data:
ts = extract_timestamp(line)
while compare(ts):
sleep(1)
else:
print("Next event")
continue
print("Finished")
这将打印一个倒计时语句,说明需要多长时间,请稍等。否则它将转到下一个事件,直到检查所有行。
您需要确保文件日期是递增的(例如,新行总是晚于前面的行)。
示例输出(我手动更改了最后一行中的日期):
Next event
Next event
Next event
Countdown: 0:00:03.531014
Countdown: 0:00:02.526724
Countdown: 0:00:01.524277
Countdown: 0:00:00.518995
Finished