Converting DDMMYYYY from dateutil.parser

I have the following string entry: 24052017

. When I try to do:

>>>dateutil.parser.parse("24052017")

      

This tells me that month must be in 1..12

.

I even tried to do:

>>>dateutil.parser.parse("24052017", firstday=True)

      

This gives me exactly the same result.

What seems to happen is that it doesn't like the fact that there are no spaces or delimiters. he reads the day correctly, but when it comes to the month he reads 0520

. This is what I suspect, at least.

How can I convert this particular input with dateutil.parser

without manipulating the string?

+3


source to share


6 answers


This format is currently not supported dateutil

. In general, if you know your date format and don't have time zones, you should just use datetime.datetime.strptime

to parse your dates as it dateutil.parser.parse

has a significant amount of overhead that it uses trying to figure out what format your date is included in and, critically, this may result in an incorrect format.

There is a pull request with a branch 2.6.0

under discussion to add this format you can find here on dateutil

github
. The main argument against this is that if you are trying to parse a series of dates, it will interpret 12052017

as "December 5, 2017" but 13052017

as "May 13, 2017". (However, you have the same inconsistency that the first date will be parsed before December 5, 2017, but the second date will just fail.)

If you don't know the format of the string, but you know that if it is an 8-digit numeric date, you want to be interpreted as DDMMYYYY

, at the moment your best bet is to hard-code this exception into your parser:



from dateutil.parser import parse as duparse
from datetime import datetime

def parse(dtstr, *args, **kwargs):
    if len(dtstr) == 8 and dtstr.isnumeric():
        return datetime.strptime(dtstr, '%d%m%Y')
    else:
        return duparse(dtstr, *args, **kwargs)

      

There are some slow planned efforts to provide a more flexible and extensible parser for dateutil

, but much work has yet to be done.

+4


source


If you don't appreciate the use dateutil

, you can do it with datetime.datetime.strptime

:

from datetime import datetime

print datetime.strptime("24052017", '%d%m%Y')

      



This returns (in yyyy-mm-dd hh: mm: ss)

2017-05-24 00:00:00

      

+6


source


Well, it dateutil.parser.parse

needs some hints about the date format you are trying to parse; in the absence of such hints, it takes the format YYYYMMDD, so your input becomes equivalent 2405-20-17

; either rebuild the string to read 20170524

, or use delimiters: dateutil.parser.parse("24.05.2017")

will work.

+1


source


You have to use the library datetime

as mentioned in asongtoruin ' answer . But if you want to achieve this using dateutil.parser

, you first need to convert the string to a format that the dateutil

. Below is an example:

>>> d_string = "24052017"

#                                                    to consider day before month v
>>> dateutil.parser.parse('/'.join([d_string[:2], d_string[2:4],d_string[4:]]), dayfirst=True)
datetime.datetime(2017, 5, 24, 0, 0)

      

Here I am converting "24052017"

to "24/05/2017"

before it gets transferred to dateutil.parser.parse(...)

.

+1


source


If you insist on using dateutil.parser.parse

, I would suggest going like this:

d = '24052017'
dateutil.parser.parse(d[4:8]+d[2:4]+d[0:2])

      

+1


source


You cannot use dateutil.parser.parse without string manipulation.

import dateutil.parser

parserinfo = dateutil.parser.parserinfo(dayfirst=True, yearfirst=False)
print dateutil.parser.parse("24052017", parserinfo)

> Traceback (most recent call last):
> File "python", line 4, in <module>
> ValueError: month must be in 1..12

      

http://dateutil.readthedocs.io/en/stable/parser.html#dateutil.parser.parserinfo

Internally, parserinfo JUMP is an array of delimiters.

# m from a.m/p.m, t from ISO T separator
JUMP = [" ", ".", ",", ";", "-", "/", "'",
        "at", "on", "and", "ad", "m", "t", "of",
        "st", "nd", "rd", "th"]

      

The empty string is not part of it.

-1


source







All Articles