简体   繁体   English

使用三个不同的日期类型库检查日期

[英]Checking a date with three different date type libraries

I have a list of different dates.我有一个不同日期的列表。 I want to test if the value in list is valid date or not.我想测试列表中的值是否为有效日期。 I have tried to do it but the statement which I gave isn't working well.我曾尝试这样做,但我给出的声明效果不佳。 How do I do it?我该怎么做?

list1 = ['201222020','20-12-2020','12122020', '53122020']
valid=[]
invalid=[]
for l in list1:
    try:
        x = dateutil.parser.parse(str(l)) or dt.strptime(str(l),'%m%d%Y') or dt.strptime(str(l),'%d%m%Y')
        valid.append(x)
    
    except:
        invalid.append(l)
print(valid)
print(invalid)

Which gives:这使:

valid = [datetime.datetime(2020, 12, 20, 0, 0)]
invalid = ['201222020', '12122020', '53122020']

But in real life dates like this '201222020','12122020' should also be accepted, which happens by using the other statement after or and this value '53122020' should be rejected.但在现实生活中,像'201222020','12122020'这样的日期也应该被接受,这是通过在or之后使用另一个语句来实现的,并且这个值'53122020'应该被拒绝。

In my scenario it's coming as above.在我的场景中,它如上所示。 Where am I going wrong?我哪里错了? How to align the statements in correct way so that the list will be checked one after the other (first date until and then datetime('%m%d%Y') and then datetime('%d%m%Y') by all three?如何以正确的方式对齐语句,以便一个接一个地检查列表(first date until and then datetime('%m%d%Y') ,然后是datetime('%d%m%Y')三个都?

Actual output:实际 output:

 valid = [datetime.datetime(2020, 20, 2, 0, 0), datetime.datetime(2020, 12, 20, 0, 0), datetime.datetime(2020, 12, 12, 0, 0)]
invalid = ['53122020']

You could create a function that tries a set of other functions to parse the input.您可以创建一个 function 来尝试一组其他函数来解析输入。 Those latter functions could be dateutil.parser.parse , datetime.strptime or whatever you choose.后面的那些函数可以是dateutil.parser.parsedatetime.strptime或任何你选择的。 If all fail, the input is invalid.如果全部失败,则输入无效。

Ex:前任:

from datetime import datetime
import dateutil

def validate(datestring, vd_funcs, vd_args):
    """
    a function that tests if a datestring can be parsed by one of the functions
    supplied by vd_funcs. vd_args can be used to provide these funcs with additional
    arguments.
    """
    for f, a in zip(vd_funcs, vd_args):
        try:
            return f(datestring, *a) # see if the function works, return if so
        except ValueError:
            continue # try next
    else:
        return None # no function worked, invalid date string
    
# the functions you want to use to see if they parse the string    
vd_funcs = (dateutil.parser.parse, datetime.strptime, datetime.strptime)
# arguments to these functions, after passing the date string itself.
vd_args = ((None,), ('%m%d%Y',), ('%d%m%Y',))    

running this for the list of example date strings gives为示例日期字符串列表运行此命令给出

valid=[]
invalid=[]  
for s in ['201222020','20-12-2020','12122020', '53122020']:
    vd = validate(s, vd_funcs, vd_args)
    if vd:
        valid.append(vd)
    else:
        invalid.append(s)
    
valid
[datetime.datetime(2020, 12, 20, 0, 0), datetime.datetime(2020, 12, 12, 0, 0)]

invalid
['201222020', '53122020']

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

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