等效于Python中Excel的Weeknum函数 [英] Equivalent of Excel's Weeknum function in Python

查看:415
本文介绍了等效于Python中Excel的Weeknum函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使目前在Excel中处理的报告自动化.作为此过程的一部分,我想要一个与Excel的Weeknum函数等效的Python(使用系统1.

I am working on automating a report which is presently handled in Excel. As part of this, I wanted a Python equivalent of Excel's Weeknum function (Using System 1. Reference here ) which considers the week having 1st Jan as Week 1.

PS:我已经尝试过ISOCalendar,但是由于星期从星期一开始,所以给出了错误的星期.我还尝试了strftime(%U"),它返回了相同的错误数字.

PS: I already tried ISOCalendar but it gives a wrong week as its week starts from Monday. I also tried the strftime("%U") and it returns the same wrong number.

有人可以帮忙吗?

推荐答案

这是伪代码.您可以将其放入Python. 您将定义一个函数Weeknum,该函数将日期d作为其输入并返回1到53之间的一个数字. 您将使用平日功能来确定第一周缺多少天.因此,如果1月1日是一周中的第一天,则空天数是0.如果1月1日是一周中的最后一天,则空天数是6.有几种方法可以做到这一点,具体取决于关于一周中的第一天与工作日函数的约定的对应程度的一些知识.最坏的情况是,您可以通过将计数器设置为1并将日期变量设置为一年中的1月1日来计算第一周的天数,而日期中的日期不是一周的最后一天,则将其设置为计数器和日期.那么短的天数是7减去计数器. 获取d年中1至366之间的数字j.一种方法是,使d与d年的1月1日之间的天数相差1+. 然后Weeknum应该返回(j + 6 +短的天数)div 7.

Here is the pseudocode. You can make it into Python. You'll define a function Weeknum that takes a date d as its input and returns a number between 1 and 53. You will use the weekday function to determine how many days the first week is short. So if January 1 is on the first day of the week, the number of days short is 0. If January 1 is the last day of the week, the number of days short is 6. There are a few ways to do that, depending a little on how well the first day of the week maps on to the conventions of the weekday function. Worst case you can count the days of the first week by setting a counter to 1 and a date variable to January 1 of the year, and while the day of the date is not the last day of the week and one to the counter and to the date. Then the number of days short is 7 minus the counter. Get the number j between 1 and 366 for the day of the year of d. One way to do so is to take 1+ the difference in days between d and January 1 of the year of d. Then Weeknum should return (j+6+number of days short) div 7.

我用Python编写了

I wrote it up in Python

import datetime
def julian(d):#takes a date d and returns what day in the year it is 1..366
    jan1 = datetime.date(d.year,1,1)
    return 1+(d-jan1).days
def daysInFirstWeekOfJanuary(y):
    #takes a year and says how many days there were in the first week of #january that year
    janDay = datetime.date(y,1,1)
    result = 1
    while (janDay.weekday()!=5):#until Saturday, change if you hold Sunday is not the first day of the week
        result=result+1
        janDay=janDay+datetime.timedelta(days=1)
    return result
def daysShortInFirstWeekOfJanuary(y):
    return 7-daysInFirstWeekOfJanuary(y)
def weeknum(d):#takes a date and returns the week number in the year
#where Jan 1 of the year is the start of week 1, and the following Sunday starts week 2
     return(julian(d)+6+daysShortInFirstWeekOfJanuary(d.year)) // 7

这篇关于等效于Python中Excel的Weeknum函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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