strftime return value encoding (mbcs, locale, etc.)

"Martin v. Löwis" martin at v.loewis.de
Sun Jan 27 17:25:00 EST 2008


>   LookupError: unknown encoding: cp932

What Python version are you using? cp932 is supported cross-platform
since Python 2.4.

> So: what is the correct code to achieve this? Will something like this 
> work:
> 
>   data = strftime("%#c", localtime())
>   if os.name == "nt":
>      data = data.decode("mbcs")
>   else:
>      data = dada.decode(locale.getpreferredencoding())
>
> Is this the correct way of doing it?

Not necessarily. On some systems, and in some locales, Python will not
have any codec that converts the locale's encoding to Unicode.

In such a case, using ASCII with replacement characters might be the
best bet, as long as the locale's charset is an ASCII superset (i.e.
you don't work on an EBCDIC machine).

> Shouldn't Python automatically alias whatever is returned by 
> locale.getpreferredencoding() to "mbcs", so that my original code works 
> portably?

No. The "mbcs" codec has a slightly different semantics from the cp932
codec, on your system. Specifically, the "mbcs" codec might map
characters as approximations, whereas the cp932 codec will give errors
if a certain Unicode character is not supported in the target character
set.

Regards,
Martin



More information about the Python-list mailing list