Converting datetime.ctime() values to Unicode

Posted by Malcolm on Stack Overflow See other posts from Stack Overflow or by Malcolm
Published on 2010-05-18T22:26:17Z Indexed on 2010/05/18 22:30 UTC
Read the original article Hit count: 264

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about python

Related posts about datetime