SyntaxError: Non-ASCII character '\xe8' in file a1.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
问题
File "a1.py", line 1
SyntaxError: Non-ASCII character '\xe8' in file a1.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
在ubuntun中运行 python a1.py,出现如上错误,根据提示查看 所给连接 http://python.org/dev/peps/pep-0263/
网页快照↓(网页翻译_中文)
网页快照(英文)
解决方案
在 .py 源码文件中首行加入
# coding=<encoding name>
或者#!/usr/bin/python # -*- coding: <encoding name> -*-
或者#!/usr/bin/python # vim: set fileencoding=<encoding name> :
主要是满足如下正则表达式^[ \t\f]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)
示例
在文件的首行添加如下注释:
# encoding=utf-8
或
# encoding:utf-8
或
# coding=utf-8
或
# coding:utf-8