Pycharm報錯 : SyntaxError: Non-ASCII character 解決方法

最近重新裝了一下pycharm出現了很多問題 素材///心塞塞,

如果讓你重新來過
你還亂刪註冊表不

言歸正傳

如果python文件中有中文字符,且未聲明編碼,就會出現報錯信息: File “encode.py”, line 1SyntaxError: Non-ASCII character ‘\xe7’ in file encode.py on line 1, but no encoding declared; see //…python…/dev/peps/pep-0263/ for details for details

根據錯誤提示,我們在python官網得到如下幫助信息:
1
Python will default to ASCII as standard encoding if no other encoding hints are given.

翻譯:如果沒有其他編碼提示,Python將默認爲ASCII作爲標準編碼。

To define a source code encoding, a magic comment must be placed into the source files either as first or second line in the file

翻譯:要定義源代碼編碼,必須在源文件中第一行或第二行放置一個魔術註釋(就像這樣)
2

所以,按照幫助文檔的提示以及例子,我們在Python文件中加入一個編碼聲明:

# coding=utf-8

在文檔開頭就好啦,問題解決√

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章