![](/img/trans.png)
[英]Non-UTF-8 code starting with '\xff' when when running setup.py
[英]Program throws (Non-UTF-8 code starting with '\xff' in file) when running from command prompt
下面是代碼:
def add(a,b):
return a + b
while True:
try:
a=float(input('Please enter a number'))
break
except:
print ('Invalid Number, please re-enter')
while True:
try:
b=float(input('Please enter a number'))
break
except:
print ('Invalid Number, please re-enter')
print(add(a,b))
我收到一個錯誤,因為我在 PowerShell 中創建了一個 python 文件,但我正在 VS 代碼中編輯它。 顯然他們使用沖突的編碼模式。 我的解決方案:我刪除了原始文件並使用 VS 代碼創建了一個新文件。 這是詳細解釋問題所在的鏈接。 https://docs.microsoft.com/en-us/powershell/scripting/dev-cross-plat/vscode/understanding-file-encoding?view=powershell-7.1
我認為問題可能在於您如何從命令行調用它。 您應該將目錄更改為 Python 解釋器所在的位置,然后將其指向腳本
c:\users\test>cd c:\python2.7
c:\python2.7>python.exe "c:\users\test\desktop\test_script.py"
只需使用 python exe 的路徑,然后用腳本的路徑替換我的桌面。
我遇到了問題
就像上面的評論所說,我也從 PowerShell 創建了該文件
問題是您的 .py 文件采用 utf-16 編碼。 在運行 python 腳本 (.py) 文件時,默認情況下它應該是 utf-8 編碼。
就我而言:我使用 cmd/PowerShell 提示符創建了一個 .py 文件
CMD prompt> echo #my First program > firstProgram.py
要解決此問題,請在記事本中打開文件,選擇save as
並將編碼從 utf-16 更改為 utf-8。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.