龙之吻:
感谢楼上各位牛的热情解答..我在程序中的路径都编程unicode了,开发机没有问题,到了没有装python的机器,报错的为之是import语句。这个怎么处理?
[阅读: 721] 2008-08-29 10:16:07
#!/usr/bin/env python
#coding=utf-8
import os
import codecs
import locale
import types
#def GetCurrDir(): return os.path.dirname("cndevOE.exe")
try:
defaultencoding = locale.getdefaultlocale()[1]
except:
defaultencoding = None
if not defaultencoding:
defaultencoding = 'utf-8'
try:
codecs.lookup(defaultencoding)
except:
defaultencoding = 'utf-8'
try:
defaultfilesystemencoding = sys.getfilesystemencoding()
except:
defaultfilesystemencoding = None
if not defaultfilesystemencoding:
defaultfilesystemencoding = 'ascii'
try:
codecs.lookup(defaultfilesystemencoding)
except:
defaultfilesystemencoding = 'ascii'
def GetCurrDir():
path = os.path.dirname(__file__)
if os.path.isfile(path):
path = os.path.dirname(os.path.abspath(path))
#print decode_string(path)
return decode_string(path)
#return os.path.dirname("cndevOE.exe")
def GetTransforSymbol():
return {"<":"<",">":">",""":'"',"(":"(",")":")"," ":" ","@":"@","&":"&"}
def unicode_path(path, encoding=defaultfilesystemencoding):
"""convert path to unicode
"""
return decode_string(path, encoding)
def (path, encoding=defaultfilesystemencoding):
return encode_string(path, encoding)
def decode_string(string, encoding=None):
"""convert string to unicode
If the second parameter encoding is omit, the default locale will be used.
"""
if not encoding:
encoding = defaultencoding
if not isinstance(string, types.UnicodeType):
return unicode(string, encoding)
else:
return string
unicode_string = decode_string
def encode_string(string, encoding=None):
"""convert unicode to string
If the second parameter encoding is omit, the default locale will be used.
"""
if not encoding:
encoding = defaultencoding
if isinstance(string, types.UnicodeType):
return string.encode(encoding)
else:
return string