创建一个 chinese.py 文件
在里面放入如下内容:
s='测试'
print s
运行提示如下错误:
sina@ubuntu:~/work/python$ python chinese.py
File "chinese.py", line 2
SyntaxError: Non-ASCII character '\xe6' in file chinese.py on line 2, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
原因是如果文件里有非ASCII字符,需要指定编码声明。把 chinese.py 文件的编码重新改为utf-8,并加上编码声明:
# -*- coding: utf-8 -*-
s='测试'
print s
此时运行正常:
sina@ubuntu:~/work/python$ python chinese.py
测试
相关参考文档:
http://cangtiandelei.blog.sohu.com/133631102.html
http://www.cnblogs.com/rollenholt/archive/2011/08/01/2123889.html