运行python程序时出现错误“SyntaxError: Non-ASCII character '\xe8' in file variable.py on line 1, but no encod”

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangjiaweiwei/article/details/78849505

创建python程序源文件:variable.py

counter = 100 #赋值整型变量

miles = 1000.0 #浮点型
name = "John" #字符串
print counter
print miles
print name



运行该python文件:

python variable.py


运行报错:

File "variable.py", line 1
SyntaxError: Non-ASCII character '\xe8' in file variable.py on line 1,

but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

看出错原因是没有编码声明,在源代码文件中添加编码声明:


# -*- coding: utf-8 -*-
counter = 100 #赋值整型变量
miles = 1000.0 #浮点型
name = "John" #字符串
print counter
print miles
print name

此时再运行程序,即可得到正确结果

python variable.py
100
1000.0
John


猜你喜欢

转载自blog.csdn.net/wangjiaweiwei/article/details/78849505