如何做一个考虑到特定月份的每一天的循环? [英] How to do a loop that takes into account each day of a specific month?

查看:52
本文介绍了如何做一个考虑到特定月份的每一天的循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个奇怪的我不知道如何解决

基本上我必须运行一段代码,此代码从文件中提取数据,该文件的名称格式为 year-month-day-hour-00-00-consensus

所以我想做的是完成一个循环,在代码运行后增加一个小时,然后到午夜就增加了一天等等,但是,尽管我有这个工作,但我不知道该怎么做对于几个月,好像很容易,因为所有月份都是 30 天,例如这很简单,我在定义一个月中的天数时遇到问题,有人有任何想法吗?

这是到目前为止的代码:

  defcalculate_and_write_hsdir(h,d,m,y):如果h <24:如果d <10:d ="0" + str(d)如果h <10:h ="0" + str(h)如果m <10:m ="0" + str(m)共识文件名= str(y)+-" + str(m)+-" + str(d)+-" + str(h)+"-00-00-共识"打印共识文件名..... 做东西 ....h =整数(h)+ 1别的:h = 00d = int(d)+1#d = d + 1 

我预先设置为:

  h = 00#小时d = 01#天m = 10#月y = 2013 #Yearcompute_and_write_hsdir(h,d,m,y)#def run_calculate(h,d,m,y):#如果m == 02:#如果d == 28:#calculate_and_write_hsdir(h,d,m,y) 

我想从2013年10月1日开始到今天结束,我该如何实现呢?很抱歉,如果这有点令人困惑,但在解释我想要它实现的目标上很困难

解决方案

Python有一个 datetime 模块,它将为您处理此问题.您创建一个表示日期和时间的 datetime 对象,以及一个表示1小时加法的 timedelta 对象.然后,您可以根据需要通过比较 datetime 对象的 .day 属性来检查新日期是否与原始日期在同一天.

datetime 还有一个便捷的方法

got a weird one i can not figure out how to solve

basically i have to run a section of code, this code extracts data from a file, which name is the format year-month-day-hour-00-00-consensus

so what i am trying to do is complete a loop that after the code runs adds an hour then once it gets to midnight adds a day etc, however while i have this working, i can not figure out how i can do this for the months as if it was easy as all months being 30 days for example this would be simple, i am having issues defining the length of days in the month, does any one have any ideas ?

this is the code so far :

def calculate_and_write_hsdir(h,d,m,y):

    if h < 24:

        if d < 10:
            d = "0"+str(d)
        if h < 10:
            h = "0"+str(h)
        if m < 10:
            m = "0"+str(m)

        consensus_file_name = str(y) + "-" + str(m) + "-" + str(d) + "-" + str(h) + "-00-00-consensus"
        print consensus_file_name 


..... do stuff ....

        h = int(h) + 1 
    else:
        h = 00
        d = int(d) + 1
            #   d = d + 1

i pre set this by :

h = 00 #Hour
d = 01 #Day
m = 10 #Month
y = 2013 #Year

calculate_and_write_hsdir(h,d,m,y)

# def run_calculate(h,d,m,y):
#   if m == 02:
#       if d == 28:
#           calculate_and_write_hsdir(h,d,m,y)

i want to start at 2013-10-01 and end at the present day, how can i achieve this ? sorry if it is a bit confusing but struggle on explaining what i want it to achieve

解决方案

Python has a datetime module that will handle this for you. You make a datetime object representing the date and time, and a timedelta object to represent the addition of 1 hour. You can then, if you need to, check whether the new date is within the same day as the original date by comparing the .day property of the datetime object.

datetime also has a convenient method strftime for printing formatted output based on the date and time.

from datetime import datetime, timedelta

def calculate_and_write_hsdir(h,d,m,y):
    before = datetime(hour = h, day = d, month = m, year = y)
    now = before + timedelta(hours = 1)
    if before.day == now.day:
        print 'still the same day'
        # ... do stuff ...
    else:
        print "now it's a different day"
        # ... do other stuff ... 
    print now.strftime('%Y-%m-%d-%H-00-00-consensus')    

h = 00 #Hour
d = 01 #Day
m = 10 #Month
y = 2013 #Year
calculate_and_write_hsdir(h, d, m, y)

这篇关于如何做一个考虑到特定月份的每一天的循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆