简体   繁体   English

在Python calendar.monthrange中使用变量时出错

[英]Error when using variable in Python calendar.monthrange

I am trying to print the number of days in a month using the Python calendar.monthrange function. 我正在尝试使用Python calendar.monthrange函数打印一个月中的calendar.monthrange数。

I have a script that runs via cron on the 1st of every month and need to return the number of days from the previous month. 我有一个脚本,该脚本在每个月的1日通过cron运行,并且需要返回上个月的天数。 I also use the arrow module to better handle and format any date/time calls that I use throughout my scripts. 我还使用箭头模块更好地处理和格式化我在整个脚本中使用的任何日期/时间调用。

Because I need to find the number of days in the previous month, which is only back one day from the date the script is being run I just subtract 1 one via timedelta. 因为我需要找到上个月的天数,而该天距离脚本运行的日期只有一天了,所以我只需通过timedelta减去1天即可。

Here is a very reduced version of my code but this is the working part: 这是我的代码的简化版本,但这是工作部分:

import calendar
import arrow
import time
from datetime import date, timedelta

yesterday = date.today() - timedelta(1)

cYfDate = yesterday.strftime('%Y.%m.%d')
cYfDateY = arrow.get(cYfDate).format('YYYY')
cYfDateM = arrow.get(cYfDate).format('M')

print cYfDateY
print cYfDateM

print calendar.monthrange(cYfDateY + ',' + cYfDateM)[1]

The first two print lines work perfectly and display the correct year & Month but the third print throws an error: 前两个打印行可以正常工作,并显示正确的年份和月份,但第三行则抛出错误:

Traceback (most recent call last):
  File "/home/pi/SystemChecker.py", line 15, in <module>
    print calendar.monthrange(cYfDateY + ',' + cYfDateM)
TypeError: monthrange() takes exactly 2 arguments (1 given)

I cannot see why it is telling me there is only 1 argument when there are clearly two there. 我不明白为什么告诉我只有一个论点,而显然有两个论点。

calendar.monthrange() takes 2 arguments, year and month . calendar.monthrange()需要2个参数, yearmonth Check the documentation here for more details: 在此处查看文档以了解更多详细信息:

calendar.monthrange(year, month) calendar.monthrange(年,月)

The immediate problem is that monthrange is expecting two arguments; 眼前的问题是, monthrange需要两个参数。 but you for some reason are taking the two arguments you have and joining them both into a single string. 但由于某种原因,您将拥有的两个参数合并为一个字符串。 Don't do that; 不要那样做 just pass the arguments: 只需传递参数即可:

calendar.monthrange(cYfDateY, cYfDateM)

The rest of the script is strange too, though. 但是,脚本的其余部分也很奇怪。 You correctly calculate yesterday's date, but then you convert it to a string, and then use the third-party arrow library to get the month and day from that string. 您正确计算了昨天的日期,但随后将其转换为字符串,然后使用第三方arrow库从该字符串获取月和日。 This won't even work, since those are now strings. 这甚至不起作用,因为它们现在是字符串。 Why are you doing this? 你为什么做这个? Why not just get the month and date you already had? 为什么不仅仅获取您已有的月份和日期?

yesterday = date.today() - timedelta(1)   
cYfDateY = yesterday.year
cYfDateM = yesterday.month

Change print calendar.monthrange(cYfDateY + ',' + cYfDateM)[1] 更改print calendar.monthrange(cYfDateY + ',' + cYfDateM)[1]
To `print calendar.monthrange(cYfDateY, cYfDateM)[1] 要打印出calendar.monthrange(cYfDateY,cYfDateM)[1]

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

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