Converting datetime.ctime() values to Unicode
I would like to convert datetime.ctime() values to Unicode.
Using Python 2.6.4 running under Windows I can set my locale to Spanish like below:
>>> import locale >>> locale.setlocale(locale.LC_ALL, 'esp' )
Then I can pass %a, %A, %b, and %B to ctime() to get day and month names and abbreviations.
>>> import datetime >>> dateValue = datetime.date( 2010, 5, 15 ) >>> dayName = dateValue.strftime( '%A' ) >>> dayName 's\xe1bado'
How do I convert the 's\xe1bado' value to Unicode? Specifically what encoding do I use?
I'm thinking I might do something like the following, but I'm not sure this is the right approach.
>>> codePage = locale.getdefaultlocale()[ 1 ] >>> d开发者_运维问答ayNameUnicode = unicode( dayName, codePage ) >>> dayNameUnicode u's\xe1bado'
Malcolm
Converting with unicode()
or string.decode()
like in your example should work. The only problem should be that in your example you use the default locale's encoding even though you set the locale to something different before. If you use locale.getlocale()[1]
instead of locale.getdefaultlocale()[1]
you should get the correct results.
It is Unicode - when you called unicode()
on it it became Unicode. You can tell because there's a u
in front of the string when it's displayed with repr()
. Try print
ing it instead:
>>> d = u's\xe1bado'
>>> d
u's\xe1bado'
>>> print d
sábado
>>>
This probably depends on your OS. But the data looks like latin1.
>>> s.decode('latin1')
u's\xe1bado'
精彩评论