Home > OS >  `parser.parse()` - how to specify allowed formats (and it's order)
`parser.parse()` - how to specify allowed formats (and it's order)

Time:01-09

I want to create my own date parser. I like parser.parse() function but if I have:

date1 = '2022/11/10'
date2 = '10/11/2022'

parser.parse(date1)
parser.parse(date2)

Then the results are:

datetime.datetime(2022, 11, 10, 0, 0)
datetime.datetime(2022, 10, 11, 0, 0)

I want to always first try to parse the day with month between day and year. So the parser.parse(date2) should return November, not October. What is more I want to parse only the dates which includes days, month, and years, so parser.parse("20") should return an error and not datetime.datetime(2023, 1, 20, 0, 0). I would simply write my own function to parse dates (couple of if statements) but I'm afraid that it might works very slow with big data.

CodePudding user response:

Do you know datetime.datetime.strptime ? It looks like what you want : just call each time with a different format you want to try for the input.

import datetime as dt
from typing import Optional


def my_date_parser(some_date_string: str) -> Optional[dt.date]:
    expected_date_formats = [
        # see https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior
        "%d/%m/%Y",
        "%Y/%m/%d",
        # ...
    ]
    for expected_date_format in expected_date_formats:
        try:
            return dt.datetime.strptime(some_date_string, expected_date_format).date()
        except ValueError:
            continue  # ignore this one
    else:  # if the for-loop went through, it means that nothing matches
        return None


date1 = '2022/11/10'
date2 = '10/11/2022'
print(date1, my_date_parser(date1))  # 2022/11/10 2022-11-10
print(date2, my_date_parser(date2))  # 10/11/2022 2022-11-10
  • Related