Delete occurrences in a text

2

I have to delete from "a las.. " to leave only the date, in Python.

21-MARZO-2017 A LAS 1600 HRS
31-ENERO A LAS 1300 HRS.
30-ENERO-2017 A LAS 1600 HRS.
20/02/2017 A LAS 1200 HORAS (MEDIODíA)
17-FEBRERO A LAS 1200 HRS (MEDIODíA)
18-NOVIEMBRE A LAS 1600 HRS.
18-ENERO 2017 A LAS 1500 HRS.
31-ENERO-2017 A LAS 1600 HRS.
16-MAYO-2017 A LAS 1500 HRS.
09-FEBRERO-2017 A LAS 1500 HRS.
30-MARZO A LAS 1600 HRS

I tried the strip() function but it did not work.

x = "31-Enero-2017 a las 1600 hrs."
y = x.rstrip("a las")
print y
    
asked by Pablo Vergara Rain 14.02.2017 в 17:35
source

2 answers

4

You can use this code:

x = "21-MARZO-2017 A LAS 1600 HRS!"
y = x.find(" A LAS")
print x[:y]

Explanation:

With the Find you look for the location of " A LAS" (important the space before the "A" so that you delete it too) and save the index in y

With the x[:y] you're saying to cut the substrig from y

Result:

  

21-MARZO-2017

    
answered by 14.02.2017 / 17:42
source
1

This could also be useful for your examples:

x = "31-ENERO-2017 A LAS 1600 hrs."
print(x.split("A LAS")[0])

Split returns a list of all words according to a separator. In this case the separator is "TO LAS".

Since the first word on the list is the date, I choose the first element 0 from the list I got.

    
answered by 14.02.2017 в 20:22