這是Python的編碼問題,設置python的默認編碼爲utf8python
如何尋找python的目錄額windows
llgdeMacBook-Pro:const llg$ pythonapp
Python 2.7.10 (default, Jul 30 2016, 18:31:42) python2.7
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin編碼
Type "help", "copyright", "credits" or "license" for more information..net
>>> import site;code
>>> site.getsitepackages()orm
['/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/site-python', '/Library/Python/2.7/site-packages']utf-8
>>> get
1 |
import sys |
2 |
reload (sys) |
3 |
sys.setdefaultencoding( 'utf-8' ) |
4 |
try : |
5 |
import apport_python_hook |
6 |
except ImportError: |
7 |
pass |
8 |
else : |
9 |
apport_python_hook.install() |
若是在windows下:
能夠在Python安裝目錄下的Lib/site-packages目錄中,新建一個sitecustomize.py文件(也能夠建在其它地方,而後手工導入,建在這裏,每次啓動Python的時候設置將自動生效),內容以下:
1 |
import sys |
2 |
sys.setdefaultencoding( 'utf-8' ) #set default encoding to utf-8 |
而後能夠查看到改變已經生效
1 |
>>> import sys |
2 |
>>> sys.getdefaultencoding() |
3 |
'utf-8' |
此時運行程序,若是仍然報告以前的錯誤,只須要顯示地設定輸出的編碼
1 |
print s.encode( 'utf-8' ) |
就能夠看到正確顯示。