SyntaxError: Non-ASCII character '\xe5' in file index.py on line 6, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

时间:2021-12-02 11:08:52

python入门,hhh

在慕课网上学习python入门,编写汉诺塔的递归调用时,代码正确。但是加上注释后编译不通过

SyntaxError: Non-ASCII character '\xe5' in file index.py on line 6, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

报如下错误:

SyntaxError: Non-ASCII character '\xe5' in file index.py on line , but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

原因如下:

Python默认是以ASCII作为编码方式的,如果在自己的Python源码中包含了中文(或者其他非英语系的语言),此时即使你把自己编写的Python源文件以UTF-8格式保存了,但实际上,这依然是不行的。

解决方法:

第一行加上如下代码

# -*- coding: UTF-8 -*- 

SyntaxError: Non-ASCII character '\xe5' in file index.py on line 6, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details