Python Forum
a function to look for dates in a string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a function to look for dates in a string
#1
i'm looking for a function that would be better than what i could write. i need to locate a valid date somewhere in a string. the way i would do it just loop through the character positions positions in the string, and for all the possible lengths from longest to shortest (greedy search), test if the substring is a valid date. is there a function that can do this better or faster?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
>>> from dateutil.parser import parse
>>> parse("today is 2020-05-04", fuzzy=True)
datetime.datetime(2020, 5, 4, 0, 0)
>>> parse("today is Monday at 12:10pm", fuzzy=True)
datetime.datetime(2020, 5, 4, 12, 10)
>>> parse("Today is May 4, 2020", fuzzy=True)
datetime.datetime(2020, 5, 4, 0, 0)
Reply
#3
>>> from dateutil.parser import parse
>>> parse('my birthday may be on 12 october 1968',fuzzy=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/dateutil/parser.py", line 1182, in parse
    return DEFAULTPARSER.parse(timestr, **kwargs)
  File "/usr/lib/python3/dist-packages/dateutil/parser.py", line 559, in parse
    raise ValueError("Unknown string format")
ValueError: Unknown string format
>>>
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
(May-05-2020, 05:51 PM)Skaperen Wrote:
>>> from dateutil.parser import parse
>>> parse('my birthday may be on 12 october 1968',fuzzy=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/dateutil/parser.py", line 1182, in parse
    return DEFAULTPARSER.parse(timestr, **kwargs)
  File "/usr/lib/python3/dist-packages/dateutil/parser.py", line 559, in parse
    raise ValueError("Unknown string format")
ValueError: Unknown string format
>>>
Got this error since your string has 'may' not part of date
Reply
#5
right. but that's a reality possible string. i would need to be able to deal with anything that is not a real date.

and there might be 2 or more dates in one string (line). i need to get them all in the same order as found in the string. i'm not sure, yet, but i may need to known the column where each one starts so i can match them with other things i find on the line.

this is a project parsing some genealogy text documents like finding out when people are born and died.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
Check this http://theautomatic.net/2018/12/18/2-pac...in-python/
Reply
#7
nice page. some good stuff there.

what i need is position relationships with other recognized text like people's names and select words. some things won't always be present. some text examples might be:
Henry Tudor born 28 Jun 1491, died 28 Jan 1547, became King Henry VIII on 24 Jun 1509 at his official coronation.
Prince Henry b. 28 Jun 1491 d. Jan 1547, m. Catherine of Aragon 11 Jun 1509.
Henry Tudor (1491-1547) married his 1st wife before he became king.
the date parsing is effectively to assist the text parsing to make it trivial to spot dates and/or times in the text. other kinds of text will be spotted or recognized by other tools to reduce some historical text for genealogical purposes.

interesting! i got back a message that said there was an error posting ... it faded away before i could read it all. i only got to read about half of it. but it posted, anyway.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  passing a string to a function to be a f-string Skaperen 3 3,988 Nov-06-2020, 06:14 AM
Last Post: Gribouillis
  f-string capability in a function Skaperen 8 5,706 Dec-26-2019, 03:19 AM
Last Post: Skaperen
  function t split a string Skaperen 2 1,816 Apr-23-2019, 06:03 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020