structseq() 错误与 time.struct_time [英] structseq() error with time.struct_time

查看:34
本文介绍了structseq() 错误与 time.struct_time的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是给出错误的python脚本:

<预><代码>>>>导入时间>>>t=[ ]>>>t.append(time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0,tm_min=0,tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1))回溯(最近一次调用最后一次):文件<stdin>",第 1 行,位于 <module>类型错误:structseq() 最多接受 2 个参数(9 个给出)

这个也有同样的错误:

<预><代码>>>>导入时间>>>t=time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0,tm_min=0,tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1)回溯(最近一次调用最后一次):文件<stdin>",第 1 行,位于 <module>类型错误:structseq() 最多接受 2 个参数(给出 9 个)

解决方案

time.struct_time 期望它的第一个参数是一个包含 9 个元素的序列:

在 [58]: time.struct_time((2000,11,30,0,0,0,3,335,-1))Out[58]: time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1)

但请注意,这会过度指定日期时间.

例如,您可以将 2000 年 1 月 1 日指定为 tm_yday = 100,这显然不正确:

在 [72]: time.struct_time((2000,1,1,0,0,0,3,100,-1))Out[72]: time.struct_time(tm_year=2000, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=100, tm_isdst=-1)

因此,最好使用日期时间并调用其 timetuple() 方法来获取 time.struct_time:

In [70]: import datetime as dt在 [71]: dt.datetime(2000,11,30,0,0,0).timetuple()Out[71]: time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1)

This is the python script that gives the error:

>>> import time
>>> t=[ ]        
>>> t.append(time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0,tm_min=0,tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: structseq() takes at most 2 arguments (9 given)

This one also gives the same error:

>>> import time 
>>> t=time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0,tm_min=0,tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: structseq() takes at most 2 arguments (9 given)

解决方案

time.struct_time expects its first argument to be a sequence with 9 elements:

In [58]: time.struct_time((2000,11,30,0,0,0,3,335,-1))
Out[58]: time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1)

But note that this overspecifies the datetime.

For instance, you could specify Jan 1, 2000 as having tm_yday = 100, which is clearly not true:

In [72]: time.struct_time((2000,1,1,0,0,0,3,100,-1))
Out[72]: time.struct_time(tm_year=2000, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=100, tm_isdst=-1)

Therefore, it is probably better to use a datetime and call its timetuple() method to obtain a time.struct_time:

In [70]: import datetime as dt

In [71]: dt.datetime(2000,11,30,0,0,0).timetuple()
Out[71]: time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1)

这篇关于structseq() 错误与 time.struct_time的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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