0

How do I extract all the dates in different format from a text file using python ? For information, the text is written in french.

Ex:

[04/30/2009 blablo 06/20/98 something else 8/2/69 happen in this moment 1/25/2011 maybe this one 9/3/2002 other blablabla Janvier 2005 this year 1974]

PS : I am not able to know all the formats of dates because the function that I seek will have to apply on many texts

7
  • Try looking into regex Commented May 16, 2018 at 14:06
  • Getting the slash-separated dates is quite easy with regex: (\d+)/(\d+)/(\d+) but the textual dates will need precise definitions in order to capture them. Commented May 16, 2018 at 14:10
  • What have to tried so far? Please post your code. Commented May 16, 2018 at 14:10
  • If you are not able to know all formats the program won't be either, and also every person who is asked to help you. except perhaps you ask some AI / neural Network People.... but still: you should be able to define what you're searching for Commented May 16, 2018 at 14:11
  • Do you have a set of predefined date formats besides the ones you mentioned? Commented May 16, 2018 at 14:13

1 Answer 1

1

There's actually a python library for this, albeit in english.

Example (from the github):

string_with_dates = """
...
entries are due by January 4th, 2017 at 8:00pm
...
created 01/15/2005 by ACME Inc. and associates.
...
"""

import datefinder

matches = datefinder.find_dates(string_with_dates)

for match in matches:
    print match

Output:

2017-01-04 20:00:00
2005-01-15 00:00:00

To translate this to french, I'd suggest taking a look at top of this file, at the top of the file there is a big ol' pile of regex in English, this could be translated to French.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Aaron, but this function extract also some prices. It is not able to make difference between date and other number. ex : 8560$ --> 8560-05-17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.