0

The title is horrible, sorry. I´m redding form a text file that has a bunch of dates in this format:

30/5/2015

and also can appear obviously with 2 numbers in the month:

21/12/2014

this is pure text, is not formatted as a date. What i have to do is to control that the month section does not exceds the number 12. This is because sometimes the date changes it´s format and appear like :

2/15/2015--- meaning that the second place turns to days and the first one to months

This are the dates that i do not need. From the text i´m getting all this dates like this:

fechas = list(set(re.findall(r'\b(\d+/\d+/\d{4})\b', data))) --- i use set() because sometimes the dates appear more than one time

I tried something like this:

for fecha in fechas:
    month = fecha[2:3]

trying to slice the string, but this wouldn´t work because of the fact that if i do this i´m getting 1 number and sometimes i need 2

Does anyone have any idea on how o do this?

Any help will be really appreciated

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
NachoMiguel
  • 983
  • 2
  • 15
  • 29
  • 4
    Unless there is some logic there is no way to know which is which, what should `11/12/16` be, which is the month and which is the day? – Padraic Cunningham Aug 26 '15 at 19:54
  • My favorite day ever was 11/10/09, do you know what happened that day? – Sam Aug 26 '15 at 19:59
  • Why does the linked question not answer yours? Once you've converted the string to a `datetime` it is trivial to reformat the result as needed. – Mark Ransom Aug 26 '15 at 20:00
  • 1
    You can also split the string. dates.split("/") and then index the first element to get the middle number(s) – Ben Aug 26 '15 at 20:04
  • 1
    The linked question answers everything but how to determine which field is the month and which is the day. The answer to that is either A) `try..except`, or B) impossible (see @PadraicCunningham's comment). – TigerhawkT3 Aug 26 '15 at 20:13
  • @TigerhawkT3 Thanks, good points, esp. B). I hadn't noticed Padraic's note. Thanks for pointing it out! – Andy Kubiak Aug 27 '15 at 12:24
  • @TigerhawkT3 thanks for the comments. Yeah i will have to deal with the fact that is probable that i can not solve this...thanks anyways everybody – NachoMiguel Aug 27 '15 at 15:30

0 Answers0