Raving Rabbott
Raving Rabbott

Reputation: 61

dateutil parser: how to parse time separated with blank spaces?

I need to turn string that comes to me in the following format ' 7/ 7/2001 16 14 58' into a datetime object, but white spaces are not recognized as a time delimiter. I believe parser only recognizes [-,:/]. How can I add white spaces to dateutil?

The following code:

    from dateutil import parser
    example_datetime = ' 7/ 7/2001 16 14 58'
    parsed_datetime = parser.parse(example_datetime)

    print(parsed_datetime)

Returns this error:

ValueError: ('Unknown string format:', ' 7/ 7/2001 16 14 58')

Upvotes: 1

Views: 584

Answers (1)

kosnik
kosnik

Reputation: 2434

Why you do not use the datetime library of python and specify your format?

from datetime import datetime
parsed_datetime = datetime.strptime(example_datetime, '%d/ %m/%Y %H %M %S')
print(parsed_datetime)

out:
datetime.datetime(2001, 7, 7, 16, 14, 58)

Upvotes: 1

Related Questions