Python Learning Miscellaneous

Start to learn Python this week, from very fundamental knowledge. Mark issues I meet in this hard and happy process. :)

Encoding for Chinese

Difference systems have different method of encoding. Windows is GBK and linux is UTF8. Whether Chinese can be displayed normally is depended on the correct encoding way. The translate method is:

1
2
3
l_example = "中文" #Original GBK
print l_example #for Windos
print l_example.decode('gbk').encode('utf8') #for Linux

Pip Install Bug Fix

When installing pip package in DOS CMD, the error occurs:

1
UnicodeEncodeError: 'ascii' codec can't encode character u''\u258f' in position 8: ordinal not in range(128)

Bug fix
Insert code to $PYTHONPATH/Lib/mimetypes.py:

1
2
3
4
default_encoding = sys.getdefaultencoding() #Line 257
if sys.getdefaultencoding() != 'gbk':
reload(sys)
sys.setdefaultencoding('gbk')

Create $PYTHONPATH/Lib/site-packages/sitecustomize.py with:

1
2
import sys
sys.setdefaultencoding('gbk')