1. 程式人生 > >執行python程式時出現錯誤“SyntaxError: Non-ASCII character '\xe8' in file variable.py on line 1, but no encod”

執行python程式時出現錯誤“SyntaxError: Non-ASCII character '\xe8' in file variable.py on line 1, but no encod”

建立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