Python入門之中文亂碼

2022-04-06 03:31:50 字數 501 閱讀 2863

python入門編碼:

python 檔案中如果未指定編碼,有中文**時候,在執行過程會出現報錯:

file "test.py", line 2

syntaxerror: non-ascii character '\xe4' in file test.py on line 2, but no encoding declared; see for details

python中預設的編碼格式是 ascii 格式,在沒修改編碼格式時無法正確列印漢字,所以在讀取中文時會報錯。

解決方法為只要在檔案開頭加入# -*- coding: utf-8 -*-或者#coding=utf-8就行了。

所以如果大家再學習過程中,**中包含中文,就需要在頭部指定編碼。

注意:python3.x 原始碼檔案預設使用utf-8編碼,所以可以正常解析中文,無需指定 utf-8 編碼。

Python入門之中文亂碼

python入門編碼 python 檔案中如果未指定編碼,有中文 時候,在執行過程會出現報錯 file test.py line 2 syntaxerror non ascii character xe4 in file test.py on line 2,but no encoding decla...

Python入門之中文亂碼

python入門編碼 python 檔案中如果未指定編碼,有中文 時候,在執行過程會出現報錯 file test.py line 2 syntaxerror non ascii character xe4 in file test.py on line 2,but no encoding decla...

Servlet之中文亂碼問題 入門版

請求資料的中文問題 1 post請求提交中文 get請求建議不提交中文 只是也有方法解決 1.1post request.setcharacterencoding utf 8 解決post中的 utf 8是專案編碼,要跟專案編碼格式統一,當然也能夠選用其它編碼。1.2響應 response.setc...