python 使用 UTF-8 編碼


題記

一般我喜歡用 utf-8 編碼,在 python 怎么使用呢?

使用utf-8 文字

在 python 源碼文件中用 utf-8 文字。一般會報錯,如下:

File "F:\workspace\psh\src\test.py", line 2
SyntaxError: Non-ASCII character '\xe4' in file F:\workspace\psh\src\test.py on line 2, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

test.py 的內容:

print "你好"  

如果要正常運行在 test.py 文件前面加編碼注釋,如:

#!/usr/bin/python2.6  
# -*- coding: utf-8 -*-  
print "你好"  

或者加入:

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

更多入門教程可以參考:(http://www.go2coding.com/)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM