Reputation: 93
For example, I have a string "do something by tomorrow 9am". I am using the parsedatetime module to parse the string and get a datetime object from it. I did this according to the parsedatetime documentation:
from datetime import datetime
import parsedatetime as pdt # $ pip install parsedatetime
cal = pdt.Calendar()
string = "do something by tomorrow 9am"
time_struct = cal.parse(string)
datetimeobject = datetime(*time_struct[0][:6])
However, I want to know which part of the string it parsed to get the datetime object because I would also like to remove the date part from the string, to get "do something". python-dateutil module seems to do that, but it doesn't parse dates / times like "tomorrow" or "2 hours later".
Is there a way to do that? The input datetime can come in many formats.
Upvotes: 2
Views: 405
Reputation: 310
Having a quick look at the doc (https://bear.im/code/parsedatetime/docs/index.html) you can see:
nlp(self, inputString, sourceTime=None)
If you use it with your entry:
cal.nlp(string)
You'll find:
((datetime.datetime(2016, 7, 11, 9, 0), 3, 16, 28, 'tomorrow 9am'),)
Maybe this will help you to find the parsed part of the string in your own string and you'll be able to do the adequate manipulation to obtain what you need.
Upvotes: 2