Reputation: 423
I'm trying to parse Portugese date into datetime. Below is what I'm trying:
import locale
from datetime import datetime
locale.setlocale(locale.LC_ALL, 'pt_PT.iso88591')
date_format = '%A, %d %B %Y, %H:%M'
date_str = 'sexta-feira, 8 de setembro de 2017, 20:08'
datetime.strptime(date_str, date_format)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/lib/python2.7/_strptime.py", line 332, in _strptime
(data_string, format))
ValueError: time data 'sexta-feira, 8 de setembro de 2017, 20:08' does not
match format '%A, %d %B %Y, %H:%M'
Also tried below date_str but still getting the same error:
date_str = 'sexta-feira, 8 setembro 2017, 20:08'
What am I doing wrong here?
Upvotes: 1
Views: 803
Reputation: 18238
POSIX date and time format has limited support for different date formats. I suggest you take a look at PyICU:
from datetime import datetime
from icu import Locale, DateFormat, ICUtzinfo, TimeZone
locale = Locale('pt_PT')
tz = ICUtzinfo.getInstance('Portugal')
now = datetime.now(tz)
df = DateFormat.createDateTimeInstance(DateFormat.kFull, DateFormat.kFull, locale)
s = df.format(now)
print s
now2 = df.parse(s)
print now2
s2 = df.format(now2)
print s2
Output:
sexta-feira, 8 de setembro de 2017 às 23:26:20 Hora de verão da Europa Central
1504905980.0
sexta-feira, 8 de setembro de 2017 às 23:26:20 Hora de verão da Europa Central
I'm scraping a website so I need to convert a date string (sexta-feira, 8 de setembro de 2017, 20:08) into datetime so I can save it into database. How would I do that using PyICU?
This would require custom CLDR pattern:
df = SimpleDateFormat("EEEE, d 'de' MMMM 'de' yyy, HH:mm", locale)
print df.parse('sexta-feira, 8 de setembro de 2017, 20:08')
Upvotes: 1
Reputation: 3492
You can easily test the reverse conversion with strftime
method. For your example it looks like this (and is pretty obvious, why your strptime
doesn't work):
In [1]: import locale
In [2]: from datetime import datetime
In [3]: locale.setlocale(locale.LC_ALL, 'pt_PT.ISO-8859-1')
Out[3]: 'pt_PT.ISO-8859-1'
In [4]: date_format = '%A, %d %B %Y, %H:%M'
In [5]: datetime.now().strftime(date_format)
Out[5]: 'S\xe1bado, 09 Setembro 2017, 00:02'
Upvotes: 0