题记

一般我喜欢用 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/)

相关文章:

  • 2021-12-12
  • 2021-08-24
  • 2021-08-12
  • 2022-01-28
  • 2021-06-17
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-09
  • 2022-02-09
  • 2022-12-23
  • 2022-12-23
  • 2023-02-14
  • 2022-12-23
相关资源
相似解决方案