views:

61

answers:

3

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 ]
>>> dayNameUnicode = unicode( dayName, codePage )
>>> dayNameUnicode
u's\xe1bado'

Malcolm

A: 

This probably depends on your OS. But the data looks like latin1.

>>> s.decode('latin1')
u's\xe1bado'
Wai Yip Tung
+3  A: 

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.

sth
+2  A: 

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 printing it instead:

>>> d = u's\xe1bado'
>>> d
u's\xe1bado'
>>> print d
sábado
>>>
Daniel G
Thanks to all who replied. Daniel you are correct - I had correctly converted to Unicode, but was looking at the repr vs. output version of the value when proofing my code.
Malcolm