FAQ
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'

Feedback appreciated.

Regards,
Malcolm

Search Discussions

  • Python at May 17, 2010 at 6:14 pm
    In researching a solution, I believe locale.getpreferredencoding() might
    be a better choice (than locale.getdefaultlocale()[ 1 ]) for determining
    a system's default encoding?

    In other words change:
    codePage = locale.getdefaultlocale()[ 1 ]
    To this:
    codePage = locale.getpreferredencoding()
    ... in my original post's code (original post follows my signature).

    Malcolm

    ----- Original message -----
    From: python at bdurham.com
    To: python-list at python.org
    Date: Mon, 17 May 2010 13:54:27 -0400
    Subject: 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 ]
    dayNameUnicode = unicode( dayName, codePage )
    dayNameUnicode
    u's\xe1bado'

    Feedback appreciated.

    Regards,
    Malcolm
  • Jerry Hill at May 17, 2010 at 6:32 pm

    On Mon, May 17, 2010 at 2:14 PM, wrote:
    In researching a solution, I believe locale.getpreferredencoding() might
    be a better choice (than locale.getdefaultlocale()[ 1 ]) for determining
    a system's default encoding?
    I haven't used the locale module a lot, but it seems to me that if
    you're setting the locale with locale.setlocale(), you should get the
    encoding from locale.getlocale()[1].

    You're not really interested in the system's default encoding at this
    point, you're interested in the encoding of the locale you've
    explicitly set.

    --
    Jerry

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMay 17, '10 at 5:54p
activeMay 17, '10 at 6:32p
posts3
users2
websitepython.org

2 users in discussion

Python: 2 posts Jerry Hill: 1 post

People

Translate

site design / logo © 2022 Grokbase