0

How can I print only the last value of the Dates written in the text? The input is a text file containing a lot of dates and I want the last date of the document.

Now my output is:

['1381566     ', '562 2486 3600    ', '511', '19', '799 e incluye sellado de tiempo y firma electr', '42', '031    ', '13 de Abril de 2018    ', '1 de 1      ', '1381566        ', '482', '25', '1613', '2339', '4831', '398', '2766', '1147', '15', '2741', '20 de  ', '2018', '13 de marzo de 2018', '42006', '27 de marzo de 2018']

I only want: 27 de marzo de 2018

import re

f = open ('/Users/.../1381566.txt','r')

mensaje = f.read()
mensaje = mensaje.replace("\n","")

print re.findall(r'\d+[ \d\w]+', mensaje)
Rakesh
  • 81,458
  • 17
  • 76
  • 113
Anna Castan
  • 89
  • 1
  • 7
  • 2
    `re.findall(r'\d[ \d\w]+', mensaje)[-1]`? There are ways to achieve that with lookarounds, or with a greedy dot + capturing, but you may use what you have and just access the last matched value. – Wiktor Stribiżew Jul 24 '18 at 07:56
  • I do not speak this language but considering your output I tweaked your regular expression to match your dates. I considered that the dates in your language are always written with the word "de" inside. `\d+(\s[A-z]+){3}\s\d{4}$` the `$` means that it will only get the match which is at the end of your file. If your date is not the last thing in your file (eg, you have some other words after) just remove the `$` and use the same trick `[-1]` as the comment above me suggested – Plopp Jul 24 '18 at 08:30
  • \d+(\s[A-z]+){3}\s\d{4} if I put this the output is {de de de}, and I want the date not the word de. This language is Spanish :) – Anna Castan Jul 24 '18 at 09:32
  • and if I put re.findall(r'\d[ \d\w]+', mensaje)[-1] that one, for another text don't work... The other text is: Demás estipulaciones constan en escritura extractada. Santiago, 06 de abril de 2018. And I want that my output to be 06 Abril 2018. And sometimes the word "de" appear in some text, and another times no. – Anna Castan Jul 24 '18 at 09:42

0 Answers0