I have a string that has several date values in it, and I want to parse them all out. The string is natural language, so the best thing I've found so far is dateutil.
Unfortunately, if a string has multiple date values in it, dateutil throws an error:
>>> s = "I like peas on 2011-04-23, and I also like them on easter and my birthday, the 29th of July, 1928"
>>> parse(s, fuzzy=True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/pymodules/python2.7/dateutil/parser.py", line 697, in parse
return DEFAULTPARSER.parse(timestr, **kwargs)
File "/usr/lib/pymodules/python2.7/dateutil/parser.py", line 303, in parse
raise ValueError, "unknown string format"
ValueError: unknown string format
Any thoughts on how to parse all dates from a long string? Ideally, a list would be created, but I can handle that myself if I need to.
I'm using Python, but at this point, other languages are probably OK, if they get the job done.
PS - I guess I could recursively split the input file in the middle and try, try again until it works, but it's a hell of a hack.
While I was offline, I was bothered by the answer I posted here yesterday. Yes it did the job, but it was unnecessarily complicated and extremely inefficient.
Here's the back-of-the-envelope edition that should do a much better job!
Example usage:
It's probably worth noting that its behaviour deviates slightly from
dateutil.parser.parse
when dealing with empty/unknown strings. Dateutil will return the current day, whileparse_multiple
returns an empty list which, IMHO, is what one would expect.P.S. Just spotted MattH's updated answer which does something very similar.
Looking at it, the least hacky way would be to modify dateutil parser to have a fuzzy-multiple option.
parser._parse
takes your string, tokenizes it with_timelex
and then compares the tokens with data defined inparserinfo
.Here, if a token doesn't match anything in
parserinfo
, the parse will fail unlessfuzzy
is True.What I suggest you allow non-matches while you don't have any processed time tokens, then when you hit a non-match, process the parsed data at that point and start looking for time tokens again.
Shouldn't take too much effort.
Update
While you're waiting for your patch to get rolled in...
This is a little hacky, uses non-public functions in the library, but doesn't require modifying the library and is not trial-and-error. You might have false positives if you have any lone tokens that can be turned into floats. You might need to filter the results some more.
Yields:
Update for Dieter
Dateutil 2.1 appears to be written for compatibility with python3 and uses a "compatability" library called
six
. Something isn't right with it and it's not treatingstr
objects as text.This solution works with dateutil 2.1 if you pass strings as unicode or as file-like objects:
If you want to set option on the parserinfo, instantiate a parserinfo and pass it to the parser object. E.g:
Why not writing a regex pattern covering all the possible forms in which a date can appear, and then launching the regex to explore the text ? I presume that there are not dozen of dozens of manners to express a date in a string.
The only problem is to gather the maximum of date's expressions
I think if you put the "words" in an array, it should do the trick. With that you can verify if it is a date or no, and put in a variable.
Once you have the date you should use datetime library library.