1

I'm working on parsing log files, which have variable datetime formats in their timestamps. I am passing this string: Feb 22 08:58:24 router1 to dateutil.parser.parse() to try and extract the timestamp, like so:

>>> dateutil.parser.parse('Feb 22 08:58:24 router1', fuzzy=True) datetime.datetime(2001, 2, 22, 8, 58, 24) 

Which results in the date: February 22nd, 2001

Why is the year parsed as 2001 rather than the current year, 2019?

4
  • At a gusss, router1 is being interpreted somehow as 1 and is mapping to 2001. Do you get the same result with the 1 or router1 removed Commented Apr 7, 2019 at 20:17
  • ok, so on my tests, router 2 prints 2002, same for just 2. Commented Apr 7, 2019 at 20:19
  • Ah, it must ignore router because fuzzy=True, that's a little frustrating Commented Apr 7, 2019 at 20:20
  • 1
    It ignores the router but not the 1 next to it. You can check this by passing the fuzzy_with_tokens=True arg instead of fuzzy=True Commented Apr 7, 2019 at 20:23

1 Answer 1

2

The parser is ignoring router but not the 1 next to it. This can be checked by passing the fuzzy_with_tokens=True argument. The output results in a tuple with the first item as the datetime object representation of the timestamp and the second item as the ignored strings.

from dateutil import parser print(parser.parse('Feb 22 08:58:24 router1', fuzzy_with_tokens=True)) #Output: (datetime.datetime(2001, 2, 22, 8, 58, 24), (' ', ' router')) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.