繁体   English   中英

获取时间值列表,从当前时间到 Python 中当前时间之后的(特定)分钟

[英]Get a List of Time Values, from the Current time upto a (certain) Minutes after the current time in Python

我需要定义一个 function 以获取从当前时间后的“M”分钟到当前时间(包括当前时间)的 str 时间值的 [list],然后检查是否有任何值与时间值匹配在给定 CSV 文件的时间列中。

我想到了for 循环的想法,但不知道如何添加和 append 列表中的 M 号。 次(绝对初学者的素质)。 因此,我使用以下仅支持 M = 1 的代码进行了调整:

def Time_Check_Min(M= 1):
    #Check the current system time
    timestr = datetime.now()
    Check_TIMEnow = timestr.strftime("%H:%M")

    #Add 'M' min to Current Time
    Ahead_Time = (timestr + timedelta(minutes=M))
    Check_TIME = Ahead_Time.strftime("%H:%M")

    #Check if the current time is mentioned in the Dataframe
    if Check_TIME in df_.Time.values or Check_TIMEnow in df_.Time.values:
        return True
    else:
        return False

我需要 output 作为 ('%H:%M') 格式的列表,以便检查 CSV 中是否存在它们。 例如,将当前系统时间设为'16:50'M = 3 ,则列表应包含 4 个元素,例如:

['16:50', '16:51', '16:52', '16:53']

另外,我考虑使用between_time方法,因为我使用的是 pandas。 但同样,我不知道这是否真的有帮助。 另外,我真诚地说,我只需要解决这个问题......但我只是问了这个问题: Get a List of Time Values before a (certain) Minutes from the Current time to the Current Time in Python ,就像一个简单的。 因此,整行都是相同的(复制),只是问题被颠倒/纠正了。

我需要改变我的方法吗? 如果是,那么这是最好的方法……如果不是,如何获得该死的清单?

感谢您的帮助和理解问题...

我认为您可以使用此代码。 为 M 输入任意数量的值

from datetime import datetime
from dateutil.relativedelta import relativedelta
now=datetime.now()
current_time=now.strftime("%H:%M")
time_list=[]
M=70
for i in range(0,M+1):
    six_months = now + relativedelta(minutes=i)
    print(six_months.strftime("%H:%M"))
    time_list.append(six_months.strftime("%H:%M"))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM