[Chicago] =?GB2312?B?cmWjuiAgcmWjuiBSZTogIFVuaWNvZGVEZWNvZGVF?= =?GB2312?B?cnJvciBXaGVuIGNvbXBpbGluZyBCaXRUb3JyZW50IGluIEJpdFRvcm5hZG8=?=

Ian Bicking ianb at colorstudy.com
Mon Oct 9 19:53:29 CEST 2006


·ÅÜø ̸ wrote:
> 
> also to supplement it, my file system encoding is "mbcs",and the default 
> encoding is "ascii".

Well, I should say that you've unintentionally gotten into a rather
difficult place in Python.  Anyway, I'll suggest an expedient fix that
might work for you, but only if mbcs is a superset of ASCII.

You can do this to set the default encoding (which is currently 'ascii'):

  import sys
  reload(sys)
  sys.setdefaultencoding('mbcs')

Do this early in the process, and when Python doesn't know what the
encoding of a string is it will guess mbcs.  If mbcs is not a superset
of ASCII then this will probably cause all kinds of havoc.  Otherwise...


> thanks.
> jolley
> */·ÅÜø ̸ <jolley84 at yahoo.com.cn>/* дµÀ£º
> 
>     first, as a newbie, i may ask some foolish problems, pls be patient
>     and endurable.
>     second, i have done this after your explanation:
>      
>     from sys import getfilesystemencoding
>     path.decode(sys.getfilesystemencoding)

Note this doesn't do anything, you have to do:

  path = path.decode(sys.getfilesystemencoding())

>     path = ("\\").decode(sys.getfilesystemencoding())
>     +b.decode(sys.getfilesystemencoding())

Perhaps the best strategy is not to try to use unicode strings at all.
It's only when you mix a unicode string and an encoded non-ascii string
that you get problems.  If they are all encoded strings, it should work
(assuming your encodings are consistent -- if you use utf8 one place and
mbcs elsewhere it will be break things).

This function can be used to encode things:

  def encode_mbcs(s):
      if isinstance(s, unicode):
          return s.encode('mbcs')
      else:
          return s

Then use this around any string that is acting funny.


-- 
Ian Bicking | ianb at colorstudy.com | http://blog.ianbicking.org


More information about the Chicago mailing list