Hướng dẫn how do you tell if a string is a date in python? - làm thế nào để bạn biết nếu một chuỗi là một ngày trong python?

Hàm parse trong dateutils.parser có khả năng phân tích nhiều định dạng chuỗi ngày vào đối tượng datetime.

Nếu bạn chỉ muốn biết liệu một chuỗi cụ thể có thể biểu diễn hoặc chứa một ngày hợp lệ hay không, bạn có thể thử chức năng đơn giản sau:

from dateutil.parser import parse

def is_date(string, fuzzy=False):
    """
    Return whether the string can be interpreted as a date.

    :param string: str, string to check for date
    :param fuzzy: bool, ignore unknown tokens in string if True
    """
    try: 
        parse(string, fuzzy=fuzzy)
        return True

    except ValueError:
        return False

Sau đó bạn có:

>>> is_date("1990-12-1")
True
>>> is_date("2005/3")
True
>>> is_date("Jan 19, 1990")
True
>>> is_date("today is 2019-03-27")
False
>>> is_date("today is 2019-03-27", fuzzy=True)
True
>>> is_date("Monday at 12:01am")
True
>>> is_date("xyz_not_a_date")
False
>>> is_date("yesterday")
False

Phân tích cú pháp tùy chỉnh

parse có thể nhận ra một số chuỗi là ngày mà bạn không muốn coi là ngày. Ví dụ:

  • Phân tích cú pháp

    >>> is_date("1990-12-1")
    True
    >>> is_date("2005/3")
    True
    >>> is_date("Jan 19, 1990")
    True
    >>> is_date("today is 2019-03-27")
    False
    >>> is_date("today is 2019-03-27", fuzzy=True)
    True
    >>> is_date("Monday at 12:01am")
    True
    >>> is_date("xyz_not_a_date")
    False
    >>> is_date("yesterday")
    False
    
    0 và
    >>> is_date("1990-12-1")
    True
    >>> is_date("2005/3")
    True
    >>> is_date("Jan 19, 1990")
    True
    >>> is_date("today is 2019-03-27")
    False
    >>> is_date("today is 2019-03-27", fuzzy=True)
    True
    >>> is_date("Monday at 12:01am")
    True
    >>> is_date("xyz_not_a_date")
    False
    >>> is_date("yesterday")
    False
    
    1 sẽ trả về một đối tượng DateTime đại diện cho ngày hiện tại với ngày và năm được thay thế cho số trong chuỗi

  • >>> is_date("1990-12-1")
    True
    >>> is_date("2005/3")
    True
    >>> is_date("Jan 19, 1990")
    True
    >>> is_date("today is 2019-03-27")
    False
    >>> is_date("today is 2019-03-27", fuzzy=True)
    True
    >>> is_date("Monday at 12:01am")
    True
    >>> is_date("xyz_not_a_date")
    False
    >>> is_date("yesterday")
    False
    
    2 và
    >>> is_date("1990-12-1")
    True
    >>> is_date("2005/3")
    True
    >>> is_date("Jan 19, 1990")
    True
    >>> is_date("today is 2019-03-27")
    False
    >>> is_date("today is 2019-03-27", fuzzy=True)
    True
    >>> is_date("Monday at 12:01am")
    True
    >>> is_date("xyz_not_a_date")
    False
    >>> is_date("yesterday")
    False
    
    3 sẽ được phân tích cú pháp là
    >>> is_date("1990-12-1")
    True
    >>> is_date("2005/3")
    True
    >>> is_date("Jan 19, 1990")
    True
    >>> is_date("today is 2019-03-27")
    False
    >>> is_date("today is 2019-03-27", fuzzy=True)
    True
    >>> is_date("Monday at 12:01am")
    True
    >>> is_date("xyz_not_a_date")
    False
    >>> is_date("yesterday")
    False
    
    4.

  • >>> is_date("1990-12-1")
    True
    >>> is_date("2005/3")
    True
    >>> is_date("Jan 19, 1990")
    True
    >>> is_date("today is 2019-03-27")
    False
    >>> is_date("today is 2019-03-27", fuzzy=True)
    True
    >>> is_date("Monday at 12:01am")
    True
    >>> is_date("xyz_not_a_date")
    False
    >>> is_date("yesterday")
    False
    
    5 sẽ trả lại ngày thứ Sáu gần nhất trong tương lai.
  • Tương tự
    >>> is_date("1990-12-1")
    True
    >>> is_date("2005/3")
    True
    >>> is_date("Jan 19, 1990")
    True
    >>> is_date("today is 2019-03-27")
    False
    >>> is_date("today is 2019-03-27", fuzzy=True)
    True
    >>> is_date("Monday at 12:01am")
    True
    >>> is_date("xyz_not_a_date")
    False
    >>> is_date("yesterday")
    False
    
    6 tương ứng với ngày hiện tại với tháng thay đổi thành tháng 8.

Ngoài ra parse không nhận thức được địa phương, vì vậy không nhận ra tháng hoặc ngày trong tuần bằng các ngôn ngữ khác ngoài tiếng Anh.

Cả hai vấn đề này có thể được giải quyết ở một mức độ nào đó bằng cách sử dụng lớp

>>> is_date("1990-12-1")
True
>>> is_date("2005/3")
True
>>> is_date("Jan 19, 1990")
True
>>> is_date("today is 2019-03-27")
False
>>> is_date("today is 2019-03-27", fuzzy=True)
True
>>> is_date("Monday at 12:01am")
True
>>> is_date("xyz_not_a_date")
False
>>> is_date("yesterday")
False
8 tùy chỉnh, trong đó xác định cách thức tên tháng và ngày được công nhận:

from dateutil.parser import parserinfo

class CustomParserInfo(parserinfo):

    # three months in Spanish for illustration
    MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]

Một thể hiện của lớp này sau đó có thể được sử dụng với parse:

>>> parse("Enero 1990")
# ValueError: Unknown string format
>>> parse("Enero 1990", parserinfo=CustomParserInfo())
datetime.datetime(1990, 1, 27, 0, 0)

Phương pháp #2: Sử dụng DateUtil.Parser.Parse ()

Lưu bài viết

  • Trong đó, chúng tôi kiểm tra định dạng được xác thực bằng cách sử dụng chức năng Inbuilt khác nhau, DateUtil.parser. Điều này không cần định dạng để phát hiện một ngày.
  • from dateutil.parser import parserinfo
    
    class CustomParserInfo(parserinfo):
    
        # three months in Spanish for illustration
        MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]
    
    0 parse1
    from dateutil.parser import parserinfo
    
    class CustomParserInfo(parserinfo):
    
        # three months in Spanish for illustration
        MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]
    
    2 parse3
  • Phương pháp #2: Sử dụng DateUtil.Parser.Parse ()

    Lưu bài viết

    Đưa ra định dạng ngày và ngày chuỗi, nhiệm vụ là viết chương trình Python để kiểm tra xem ngày có hợp lệ không và khớp với định dạng.

    Examples:

    Đầu vào: test_str = xông04 -01-1997 ′, định dạng = Hồi%d-%m-%y test_str = ’04-01-1997′, format = “%d-%m-%Y”

    Đầu ra: Đúng True

    Giải thích: Định dạng phù hợp với ngày. Formats match with date.

    Đầu vào: test_str = xông04 -14-1997 ′, định dạng = Hồi%d-%m-%y test_str = ’04-14-1997′, format = “%d-%m-%Y”

    Đầu ra: Sai False

    Giải thích: Tháng không thể là 14. Month cannot be 14.

    Phương pháp số 1: Sử dụng Strptime ()

    Trong đó, hàm, Strptime thường được sử dụng để chuyển đổi ngày chuỗi sang đối tượng DateTime, được sử dụng như khi nó không khớp với định dạng hoặc ngày, tăng giá trịerror và do đó có thể được sử dụng để tính toán hiệu lực.

    Python3

    from dateutil.parser import parserinfo
    
    class CustomParserInfo(parserinfo):
    
        # three months in Spanish for illustration
        MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]
    
    0
    from dateutil.parser import parserinfo
    
    class CustomParserInfo(parserinfo):
    
        # three months in Spanish for illustration
        MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]
    
    1
    from dateutil.parser import parserinfo
    
    class CustomParserInfo(parserinfo):
    
        # three months in Spanish for illustration
        MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]
    
    2 datetime

    from dateutil.parser import parserinfo
    
    class CustomParserInfo(parserinfo):
    
        # three months in Spanish for illustration
        MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]
    
    4
    from dateutil.parser import parserinfo
    
    class CustomParserInfo(parserinfo):
    
        # three months in Spanish for illustration
        MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]
    
    5
    from dateutil.parser import parserinfo
    
    class CustomParserInfo(parserinfo):
    
        # three months in Spanish for illustration
        MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]
    
    6

    from dateutil.parser import parserinfo
    
    class CustomParserInfo(parserinfo):
    
        # three months in Spanish for illustration
        MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]
    
    7
    from dateutil.parser import parserinfo
    
    class CustomParserInfo(parserinfo):
    
        # three months in Spanish for illustration
        MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]
    
    8
    from dateutil.parser import parserinfo
    
    class CustomParserInfo(parserinfo):
    
        # three months in Spanish for illustration
        MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]
    
    9
    >>> parse("Enero 1990")
    # ValueError: Unknown string format
    >>> parse("Enero 1990", parserinfo=CustomParserInfo())
    datetime.datetime(1990, 1, 27, 0, 0)
    
    0
    >>> parse("Enero 1990")
    # ValueError: Unknown string format
    >>> parse("Enero 1990", parserinfo=CustomParserInfo())
    datetime.datetime(1990, 1, 27, 0, 0)
    
    1
    >>> parse("Enero 1990")
    # ValueError: Unknown string format
    >>> parse("Enero 1990", parserinfo=CustomParserInfo())
    datetime.datetime(1990, 1, 27, 0, 0)
    
    2

    >>> parse("Enero 1990")
    # ValueError: Unknown string format
    >>> parse("Enero 1990", parserinfo=CustomParserInfo())
    datetime.datetime(1990, 1, 27, 0, 0)
    
    3
    from dateutil.parser import parserinfo
    
    class CustomParserInfo(parserinfo):
    
        # three months in Spanish for illustration
        MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]
    
    5
    >>> parse("Enero 1990")
    # ValueError: Unknown string format
    >>> parse("Enero 1990", parserinfo=CustomParserInfo())
    datetime.datetime(1990, 1, 27, 0, 0)
    
    5

    >>> parse("Enero 1990")
    # ValueError: Unknown string format
    >>> parse("Enero 1990", parserinfo=CustomParserInfo())
    datetime.datetime(1990, 1, 27, 0, 0)
    
    6
    from dateutil.parser import parserinfo
    
    class CustomParserInfo(parserinfo):
    
        # three months in Spanish for illustration
        MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]
    
    5
    >>> parse("Enero 1990")
    # ValueError: Unknown string format
    >>> parse("Enero 1990", parserinfo=CustomParserInfo())
    datetime.datetime(1990, 1, 27, 0, 0)
    
    8

    >>> parse("Enero 1990")
    # ValueError: Unknown string format
    >>> parse("Enero 1990", parserinfo=CustomParserInfo())
    datetime.datetime(1990, 1, 27, 0, 0)
    
    9
    The original string is : 04-01-1997
    Does date match format? : True
    0

    The original string is : 04-01-1997
    Does date match format? : True
    1
    >>> parse("Enero 1990")
    # ValueError: Unknown string format
    >>> parse("Enero 1990", parserinfo=CustomParserInfo())
    datetime.datetime(1990, 1, 27, 0, 0)
    
    6
    from dateutil.parser import parserinfo
    
    class CustomParserInfo(parserinfo):
    
        # three months in Spanish for illustration
        MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]
    
    5
    The original string is : 04-01-1997
    Does date match format? : True
    4
    The original string is : 04-01-1997
    Does date match format? : True
    5
    >>> parse("Enero 1990")
    # ValueError: Unknown string format
    >>> parse("Enero 1990", parserinfo=CustomParserInfo())
    datetime.datetime(1990, 1, 27, 0, 0)
    
    3
    The original string is : 04-01-1997
    Does date match format? : True
    7

    The original string is : 04-01-1997
    Does date match format? : True
    8
    The original string is : 04-01-1997
    Does date match format? : True
    9

    The original string is : 04-01-1997
    Does date match format? : True
    1
    >>> parse("Enero 1990")
    # ValueError: Unknown string format
    >>> parse("Enero 1990", parserinfo=CustomParserInfo())
    datetime.datetime(1990, 1, 27, 0, 0)
    
    6
    from dateutil.parser import parserinfo
    
    class CustomParserInfo(parserinfo):
    
        # three months in Spanish for illustration
        MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]
    
    5
    The original string is : 04-01-1997
    Does date match format? : True
    3

    from dateutil.parser import parserinfo
    
    class CustomParserInfo(parserinfo):
    
        # three months in Spanish for illustration
        MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]
    
    7
    from dateutil.parser import parserinfo
    
    class CustomParserInfo(parserinfo):
    
        # three months in Spanish for illustration
        MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]
    
    8
    The original string is : 04-01-1997
    Does date match format? : True
    6
    >>> parse("Enero 1990")
    # ValueError: Unknown string format
    >>> parse("Enero 1990", parserinfo=CustomParserInfo())
    datetime.datetime(1990, 1, 27, 0, 0)
    
    0
    >>> parse("Enero 1990")
    # ValueError: Unknown string format
    >>> parse("Enero 1990", parserinfo=CustomParserInfo())
    datetime.datetime(1990, 1, 27, 0, 0)
    
    1
    The original string is : 04-01-1997
    Does date match format? : True
    9

    Output:

    The original string is : 04-01-1997
    Does date match format? : True

    Phương pháp #2: Sử dụng DateUtil.Parser.Parse ()

    Trong đó, chúng tôi kiểm tra định dạng được xác thực bằng cách sử dụng chức năng Inbuilt khác nhau, DateUtil.parser. Điều này không cần định dạng để phát hiện một ngày.

    Python3

    from dateutil.parser import parserinfo
    
    class CustomParserInfo(parserinfo):
    
        # three months in Spanish for illustration
        MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]
    
    0 parse1
    from dateutil.parser import parserinfo
    
    class CustomParserInfo(parserinfo):
    
        # three months in Spanish for illustration
        MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]
    
    2 parse3

    from dateutil.parser import parserinfo
    
    class CustomParserInfo(parserinfo):
    
        # three months in Spanish for illustration
        MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]
    
    4
    from dateutil.parser import parserinfo
    
    class CustomParserInfo(parserinfo):
    
        # three months in Spanish for illustration
        MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]
    
    5
    from dateutil.parser import parserinfo
    
    class CustomParserInfo(parserinfo):
    
        # three months in Spanish for illustration
        MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]
    
    6

    from dateutil.parser import parserinfo
    
    class CustomParserInfo(parserinfo):
    
        # three months in Spanish for illustration
        MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]
    
    7
    from dateutil.parser import parserinfo
    
    class CustomParserInfo(parserinfo):
    
        # three months in Spanish for illustration
        MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]
    
    8
    from dateutil.parser import parserinfo
    
    class CustomParserInfo(parserinfo):
    
        # three months in Spanish for illustration
        MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]
    
    9
    >>> parse("Enero 1990")
    # ValueError: Unknown string format
    >>> parse("Enero 1990", parserinfo=CustomParserInfo())
    datetime.datetime(1990, 1, 27, 0, 0)
    
    0
    >>> parse("Enero 1990")
    # ValueError: Unknown string format
    >>> parse("Enero 1990", parserinfo=CustomParserInfo())
    datetime.datetime(1990, 1, 27, 0, 0)
    
    1
    >>> parse("Enero 1990")
    # ValueError: Unknown string format
    >>> parse("Enero 1990", parserinfo=CustomParserInfo())
    datetime.datetime(1990, 1, 27, 0, 0)
    
    2

    >>> parse("Enero 1990")
    # ValueError: Unknown string format
    >>> parse("Enero 1990", parserinfo=CustomParserInfo())
    datetime.datetime(1990, 1, 27, 0, 0)
    
    3
    from dateutil.parser import parserinfo
    
    class CustomParserInfo(parserinfo):
    
        # three months in Spanish for illustration
        MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]
    
    5
    >>> parse("Enero 1990")
    # ValueError: Unknown string format
    >>> parse("Enero 1990", parserinfo=CustomParserInfo())
    datetime.datetime(1990, 1, 27, 0, 0)
    
    5

    >>> parse("Enero 1990")
    # ValueError: Unknown string format
    >>> parse("Enero 1990", parserinfo=CustomParserInfo())
    datetime.datetime(1990, 1, 27, 0, 0)
    
    6
    from dateutil.parser import parserinfo
    
    class CustomParserInfo(parserinfo):
    
        # three months in Spanish for illustration
        MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]
    
    5
    >>> parse("Enero 1990")
    # ValueError: Unknown string format
    >>> parse("Enero 1990", parserinfo=CustomParserInfo())
    datetime.datetime(1990, 1, 27, 0, 0)
    
    8

    >>> parse("Enero 1990")
    # ValueError: Unknown string format
    >>> parse("Enero 1990", parserinfo=CustomParserInfo())
    datetime.datetime(1990, 1, 27, 0, 0)
    
    9
    The original string is : 04-01-1997
    Does date match format? : True
    0

    The original string is : 04-01-1997
    Does date match format? : True
    1
    >>> parse("Enero 1990")
    # ValueError: Unknown string format
    >>> parse("Enero 1990", parserinfo=CustomParserInfo())
    datetime.datetime(1990, 1, 27, 0, 0)
    
    6
    from dateutil.parser import parserinfo
    
    class CustomParserInfo(parserinfo):
    
        # three months in Spanish for illustration
        MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]
    
    5
    The original string is : 04-01-1997
    Does date match format? : True
    4
    The original string is : 04-01-1997
    Does date match format? : True
    5
    >>> parse("Enero 1990")
    # ValueError: Unknown string format
    >>> parse("Enero 1990", parserinfo=CustomParserInfo())
    datetime.datetime(1990, 1, 27, 0, 0)
    
    3
    The original string is : 04-01-1997
    Does date match format? : True
    7

    The original string is : 04-01-1997
    Does date match format? : True
    8
    The original string is : 04-01-1997
    Does date match format? : True
    9

    The original string is : 04-01-1997
    Does date match format? : True
    1
    >>> parse("Enero 1990")
    # ValueError: Unknown string format
    >>> parse("Enero 1990", parserinfo=CustomParserInfo())
    datetime.datetime(1990, 1, 27, 0, 0)
    
    6
    from dateutil.parser import parserinfo
    
    class CustomParserInfo(parserinfo):
    
        # three months in Spanish for illustration
        MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]
    
    5
    The original string is : 04-01-1997
    Does date match format? : True
    3

    from dateutil.parser import parserinfo
    
    class CustomParserInfo(parserinfo):
    
        # three months in Spanish for illustration
        MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]
    
    7
    from dateutil.parser import parserinfo
    
    class CustomParserInfo(parserinfo):
    
        # three months in Spanish for illustration
        MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]
    
    8
    The original string is : 04-01-1997
    Does date match format? : True
    6
    >>> parse("Enero 1990")
    # ValueError: Unknown string format
    >>> parse("Enero 1990", parserinfo=CustomParserInfo())
    datetime.datetime(1990, 1, 27, 0, 0)
    
    0
    >>> parse("Enero 1990")
    # ValueError: Unknown string format
    >>> parse("Enero 1990", parserinfo=CustomParserInfo())
    datetime.datetime(1990, 1, 27, 0, 0)
    
    1
    The original string is : 04-01-1997
    Does date match format? : True
    9

    Output:

    The original string is : 04-01-1997
    Does date match format? : True