1

This might be a duplicated question but since I have read all the comments about it and I don't solve my question I have to ask again.

I am working with a dataframe where I Have a column with this look:

print final['fecha_dato'][2]

31 de Enero del 2017

I do this in order to make it more understandable:

d = final['fecha_dato'][2]
d = d.replace(' de ','-')
d = d.replace(' del ','-')

And i get this output: '31-Enero-2017'

I want to convert it to 31-01-2017 format and I try this in order to make the Spanish understandable:

locale.setlocale(locale.LC_ALL, 'en_US')

but it says: unsupported locale setting.

Once i've done this I guess I will have to do something like this:

datetime.strptime('6-Mar-2016', '%d-%b-%Y').strftime('%Y-%m-%d')

Let's see what is the problem. If you can give me a way of using the replace in just one sentence that would be great too. Thanks in advance!!

Saran
  • 179
  • 12
Borja_042
  • 1,071
  • 1
  • 14
  • 26
  • 1
    Possible duplicate of [Python locale error: unsupported locale setting](http://stackoverflow.com/questions/14547631/python-locale-error-unsupported-locale-setting) – dabadaba Mar 13 '17 at 09:53
  • 1
    `d = d.replace(' de ','-').replace(' del ','-')` works in a single line, and even better: `d = final['fecha_dato'][2].replace(' de ','-').replace(' del ','-')` also works. – Adirio Mar 13 '17 at 09:58
  • There's no need to use the `.replace` method, let the `.strptime` method take care of that. OTOH, if the amount of whitespace is variable you may wish to split & re-join the string to make it use single spaces (or some other separator of your choice) between fields. – PM 2Ring Mar 13 '17 at 10:05

2 Answers2

4

You are probably on Windows, where the locale code is different. First you parse the locale so you can read the date string properly, using the appropriate code:

import locale
locale.setlocale(locale.LC_ALL, 'esp_esp')

Then, as you guessed, you parse the string to get a date object using strptime, and you convert it back to a string using the format you want with strftime:

from datetime import datetime as dt
date = dt.strptime('31 de Enero del 2017', '%d de %B del %Y')
date_str = date.strftime('%Y-%m-%d')
dabadaba
  • 9,064
  • 21
  • 85
  • 155
  • I tried this but I used it on a wrong way as I was trying to do it with a wrong code on setlocales as I coulndt see the right one on the documentation. Thanks a lot! – Borja_042 Mar 13 '17 at 10:50
2

You can do this in one line because your output date format is purely numeric, assuming your OS locale database is set up properly.

from datetime import datetime
import locale 

locale.setlocale(locale.LC_ALL, 'es_ES.utf8')

print(datetime.strptime('31 de Enero del 2017', '%d de %B del %Y').strftime('%Y-%m-%d'))

output

2017-01-31

If you want to output a date containing day &/or month names you need to do it in two stages so you can switch locales.

from datetime import datetime
import locale 

locale.setlocale(locale.LC_ALL, 'es_ES.utf8')
s = '31 de Enero del 2017'
d = datetime.strptime(s, '%d de %B del %Y')

locale.setlocale(locale.LC_ALL, 'en_US.utf8')
print(d.strftime('%A, %B %d %Y'))        

output

Tuesday, January 31 2017
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182