Question about encoding, I need a clue ...

Vlastimil Brom vlastimil.brom at gmail.com
Fri Aug 5 19:52:30 EDT 2011


2011/8/5 Geoff Wright <geoffwright240 at gmail.com>:
> Hi,
>
> I use Mac OSX for development but deploy on a Linux server.  (Platform details provided below).
>
> When the locale is set to FR_CA, I am not able to display a u circumflex consistently across the two machines even though the default encoding is set to "ascii" on both machines.  ...
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Hi,
I believe, sys.getdefaultencoding() isn't relevant here;
you could try to determine the locale encoding via locale.getlocale()
- it should return a tuple with language code and the encoding name
http://docs.python.org/library/locale.html#locale.getlocale
I suppose, you get the respective encodings on both of your different systems.

I somehow can't find the Canadian locale on my OS (win XP, Czech), but
hopefully the results are equivalent with French, I checked: cf

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit
(Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import locale
>>> import calendar
>>> locale.setlocale(locale.LC_ALL,'French')
'French_France.1252'
>>> locale.getlocale()
('fr_FR', 'cp1252')
>>> calendar.month_name[8]
'ao\xfbt'
>>> print calendar.month_name[8]
aoűt
>>> unicode(calendar.month_name[8], locale.getlocale()[1])
u'ao\xfbt'
>>> print unicode(calendar.month_name[8], locale.getlocale()[1])
août
>>>

====

The above are the results in Idle and wx pyshell, i.e. unicode-enabled
shells; on non-unicode cmd shell in windows I get:
>>> print calendar.month_name[8]
aout

and even:
>>> print unicode(calendar.month_name[8], locale.getlocale()[1])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\encodings\cp852.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\xfb' in position 2
: character maps to <undefined>
>>>

hth,
   vbr



More information about the Python-list mailing list