2008年1月24日

Python 編碼

處理Python 編碼真是討厭阿
(OS:誰叫我們是非英語系的勒)
1.%安裝目錄%\Lib\site.py --->
def setencoding():
"""Set the string encoding used by the Unicode implementation. The default is 'ascii', but if you're willing to experiment, you can change this."""
encoding = "utf-8" # Default value set by _PyUnicode_Init()
-------> 可修改預設編碼值 (其sys.setdefaultencoding(encoding) 在 2.0 後無效)

2.在script上宣告編碼
#-*- coding: utf-8 -*-

3. 如果上述都不指定,則輸入的檔案要UTF-8 格式~在str.encode("編碼格式") 指定即可

4. locale.getdefaultlocale() ---> 可得到預設的系統編碼
可搭配encode使用 例如 unicode(str).encode(locale.getdefaultlocale() )

沒有留言: