我需要得到一个ISO 8601日期,它在python 3.0中只显示给定日期的周数和两位数年份代码。这需要采用以下格式:YYWW(YY表示两位数的年份代码,WW表示周数)。我曾尝试在python中使用datetime模块,并使用%G和%V使用strftime获取周数,但在运行以下代码时出现值错误:
from datetime import datetime
now_iso = (datetime.now().strftime('%G%V'))
如果您能提供任何帮助,我们将不胜感激。提前谢谢。以下是我得到的错误:
Traceback (most recent call last):
File "C:\Python27\Lib\lib-tk\Tkinter.py", line 1547, in __call__
return self.func(*args)
File "C:/Users/ctschantz/Python Project 3/Solenoids Label Program.py", line 881, in close_part
part_validation()
File "C:/Users/ctschantz/Python Project 3/Solenoids Label Program.py", line 245, in part_validation
part_label_create()
File "C:/Users/ctschantz/Python Project 3/Solenoids Label Program.py", line 58, in part_label_create
now_bc = (datetime.now().strftime('%G%V'))
ValueError: Invalid format string
没有
%G
和%V
的简明解决方案可以如下所示:{1:02}
表示将前导0
添加到索引为1
的参数,直到其宽度至少为2
。有关更多信息,请查看Format Specification Mini-Language如果年份可以用4位数字打印,则它将成为一行:
我找到了解决办法。它可能不是最漂亮的,但:
相关问题 更多 >
编程相关推荐