Python 中文編碼

2021-09-28 14:12:56 字數 546 閱讀 1611

如何用 python 輸出 「hello, world!」,英文沒有問題,但是如果你輸出中文字元"你好,世界"就有可能會碰到中文編碼問題。

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

#!/usr/bin/python

print 「你好,世界」;

以上程式執行輸出結果為:

file 「test.py」, line 2

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

以上出錯資訊顯示了我們為指定編碼,解決方法為只要在檔案開頭加入 # -- coding: utf-8 -- 或者 #coding=utf-8 就行了。

#!/usr/bin/python、# -- coding: utf-8 --

print 「你好,世界」;

輸出結果為:

你好,世界

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

Python 中文編碼

python 檔案中如果未指定編碼,在執行過程會出現報錯 usr bin python print 你好,世界 以上程式執行輸出結果為 file test.py line 2 syntaxerror non ascii character xe4 in file test.py on line 2,...

Python 中文編碼

在python中如果輸出中文字元 你好,世界 就有可能會碰到中文編碼問題。python 檔案中如果未指定編碼,在執行過程會出現報錯 usr bin python print 你好,世界 以上程式執行輸出結果為 file test.py line 2syntaxerror non ascii char...

python 中文編碼

1.在python原始碼裡出現了中文 在原始碼開頭加上字元編碼的宣告,用乙個特殊的注釋行來定義字符集。比如 coding utf 8 或 encode utf 8 2.操作中文字元 python中有兩種預設的字串 str和unicode,將字串看作是位元組序列,將字串看作是字元的序列。python內...