为什么python的'datetime.strptime'函数在使用'functools.partial'调用时的行为方式不同?



这是我面临的错误的示例:

In [1]: from functools import partial                                                                                             
In [2]: from datetime import datetime                                                                                             
In [3]: datetime.strptime("2/3/2016", "%m/%d/%Y")                                                                                 
Out[3]: datetime.datetime(2016, 2, 3, 0, 0)
In [4]: partial(datetime.strptime, "%m/%d/%Y")("2/3/2016")                                                                        
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-d803aff4879c> in <module>
----> 1 partial(datetime.strptime, "%m/%d/%Y")("2/3/2016")
~/miniconda3/envs/ROS/lib/python3.6/_strptime.py in _strptime_datetime(cls, data_string, format)
    563     """Return a class cls instance based on the input string and the
    564     format string."""
--> 565     tt, fraction = _strptime(data_string, format)
    566     tzname, gmtoff = tt[-2:]
    567     args = tt[:6] + (fraction,)
~/miniconda3/envs/ROS/lib/python3.6/_strptime.py in _strptime(data_string, format)
    360     if not found:
    361         raise ValueError("time data %r does not match format %r" %
--> 362                          (data_string, format))
    363     if len(data_string) != found.end():
    364         raise ValueError("unconverted data remains: %s" %
ValueError: time data '%m/%d/%Y' does not match format '2/3/2016'

如何使用partial才能使datetime.strptime正确地表现?这是我如何使用partial还是如何使用datetime.strptime

的问题

您将格式(应该是第二个参数(通过partial首先传递给strptime,然后传递日期字符串(应该是第一个参数(,导致错误。

您不能将datetime.strptimepartial一起使用,因为它不使用任何关键字参数。相反,您可以使用常规功能:

In [246]: def get_dt(string): 
     ...:     return datetime.strptime(string, "%m/%d/%Y") 
     ...:                                                                                                                                                                                                   
In [247]: get_dt("2/3/2016")                                                                                                                                                                                
Out[247]: datetime.datetime(2016, 2, 3, 0, 0)

确保接受beemayl的答案。我只有几件事要添加可能很有用。

的确,您看到的问题是"参数"的订购。到partial

>>> from functools import partial  
>>> from datetime import datetime  
>>> datetime.strptime("2/3/2016", "%m/%d/%Y") 
datetime.datetime(2016, 2, 3, 0, 0)
>>> partial(datetime.strptime, "%m/%d/%Y")("2/3/2016")
Traceback (most recent call last):
    ValueError: time data '%m/%d/%Y' does not match format '2/3/2016'

确实,如果您扭转了参数,则partial有效:

>>> partial(datetime.strptime, "2/3/2016")("%m/%d/%Y")
datetime.datetime(2016, 2, 3, 0, 0)

,但这不是您想要的。

所以,您可能会认为您可以在这里利用Kwargs ...实际上,如果您查看文档,它说:

classmethod dateTime.strptime(date_string,格式(

返回与date_string相对应的日期时间,根据格式解析。

所以让我们尝试一下:

>>> partial(datetime.strptime, format="%m/%d/%Y")("2/3/2016")
Traceback (most recent call last):
    TypeError: strptime() takes no keyword arguments

没有关键字参数!什么?!是的,大多数功能都来自C&quot实际上不是!默认情况下,您在python中写下自己的功能将始终具有夸尔格斯,除非您使用Python 3.8中的酷新功能,使您可以禁止它们。

有趣的是,如果您制作自己的 strptime:

>>> def my_strptime(date_string, format):
...     return datetime.strptime(date_string, format)
... 

然后您可以做您想做的事!

>>> partial(my_strptime, format="%m/%d/%Y")("2/3/2016")
datetime.datetime(2016, 2, 3, 0, 0)

提供了您使用Kwargformat

最新更新