Python 文件中如果未指定编码,在执行过程会出现报错:
#!/usr/bin/pythonprint "你好,世界";
以上程序执行输出结果为:
File "test.py", line 2SyntaxError: Non-ASCII character '\xe4' in file test.py on line 2, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
以上出错信息显示了我们为指定编码,解决方法为只要在文件开头加入 # -*- coding: UTF-8 -*- 或者 #coding=utf-8 就行了。
#coding=utf-8#!/usr/bin/pythonprint "你好,世界";
输出结果为:
你好,世界