I couldn't convert this date in the Brazilian standard to a date in Python. I tried the code below but it didn't work.
import datetime
datetime.datetime.strptime('5 de fevereiro de 2022', '%d-%b-%y').date()
ValueError: time data '5 de fevereiro de 2022' does not match format '%d-%b-%y'
My goal is to convert all dates in this format that are in a list, like this one below:
['app', '5 de fevereiro de 2022', '6 de fevereiro de 2022', '7 de fevereiro de 2022', '8 de fevereiro de 2022']
CodePudding user response:
You can try with another library, dateparser
import dateparser
values = ['app', '5 de fevereiro de 2022', '6 de fevereiro de 2022', '7 de fevereiro de 2022', '8 de fevereiro de 2022']
[dateparser.parse(x) for x in values]
Output:
[None,
datetime.datetime(2022, 2, 5, 0, 0),
datetime.datetime(2022, 2, 6, 0, 0),
datetime.datetime(2022, 2, 7, 0, 0),
datetime.datetime(2022, 2, 8, 0, 0)]
CodePudding user response:
set your locale:
import datetime
import locale
locale.setlocale(locale.LC_TIME, "portuguese_brazil") # lc name is platform-specific
datetime.datetime.strptime('5 de fevereiro de 2022', '%d de %B de %Y').date()
# datetime.date(2022, 2, 5)
...and most importantly, use the correct parsing directive.