1629. 按鍵持續時間最長的鍵
1917
2025-03-31
目錄
文章目錄
目錄
基本概念
Python 的字符串
Python 的編碼(encode)與解碼(decode)
基本概念
bit(比特):計算機中最小的數據單位。
byte(字節):計算機存儲數據的單元。
char(字符):人類能夠識別的符號。
string(字符串):由 char 組成的字符序列。
bytecode(字節碼):以 byte 的形式存儲 char 或 string。
encode(編碼):將人類可識別的 char 或 string 轉換為機器可識別的 bytecode。存在多種轉換格式,例如:Unicode、ASCII、UTF-8、GBK 等類型。
decode(解碼):encode 的反向過程。
Python 的字符串
Python 具有兩種不同的 String,一種存儲文本,一種存儲字節。
P2 默認的編碼格式是 ASCII,但因為 ASCII 只支持數百個字符,不能靈活支持中文等非英文字符,所以 P2 同時還支持了 Unicode 這種更強大的編碼格式。但由于 P2 同時支持了兩套編碼格式,就難免多出了一些 encode/decode 的麻煩。
為此,P3 則統一使用了 Unicode 編碼格式,帶來了很大的開發便利。
P2:
對于文本:采用 Unicode 存儲。
對于字節:采用原始字節序列或者 ASCII 存儲。
P3:
3. 對于文本:采用 Unicode 存儲,被命名為 str。
4. 對于字節:采用 Unicode 存儲,被命名為 bytes。
所以,P2 和 P3 的 build-in str() 也不相同:
P2 str():
elp on class str in module __builtin__: class str(basestring) | str(object='') -> string | | Return a nice string representation of the object. | If the argument is a string, the return value is the same object.
1
2
3
4
5
6
7
P3 str():
Help on class str in module builtins: class str(object) | str(object='') -> str | str(bytes_or_buffer[, encoding[, errors]]) -> str | | Create a new string object from the given object. If encoding or | errors is specified, then the object must expose a data buffer | that will be decoded using the given encoding and error handler. | Otherwise, returns the result of object.__str__() (if defined) | or repr(object). | encoding defaults to sys.getdefaultencoding(). | errors defaults to 'strict'.
1
2
3
4
5
6
7
8
9
10
11
12
13
Python 的編碼(encode)與解碼(decode)
由于,P3 的 string 均為 unicode 編碼,因此在做 encode/decode 轉換時,會以 unicode 作為中間編碼,即:先將其他編碼的字符串解碼(decode)成 unicode,再從 unicode 編碼(encode)成另一種編碼。
編碼(encode):將 unicode str 轉換為特定編碼格式的 bytecode 并存儲,例如:將 unicode str1 轉換成 gb2312 bytecode。
解碼(decode):將特定編碼格式的 bytecode 轉換為 unicode str 的過程,例如:將 gb2312 bytecode 換成 unicode str2。
舉例來說:
當我們用 VIM 編輯器打開一個 .py 文件,輸入代碼 a = 123,那么這個 a = 123 就是一個 unicode str。當我們保存文件后,這個 str 就會根據 VIM 的設置被轉換為對應的編碼格式(e.g. utf8)的 bytecode 保存到系統的硬盤,這是一個 encode 過程;
然后,當 Python 解釋器執行 .py 文件時,先將 bytecode 按照指定的編碼格式 decode 為 unicode str,然后運行程序,這是一個 decode 過程。
>>> '美麗人生'.encode('gbk') b'\xc3\xc0\xc0\xf6\xc8\xcb\xc9\xfa' >>> b'\xc3\xc0\xc0\xf6\xc8\xcb\xc9\xfa'.decode('gbk') '美麗人生' >>> '美麗人生'.encode('utf-8') b'\xe7\xbe\x8e\xe4\xb8\xbd\xe4\xba\xba\xe7\x94\x9f' >>> b'\xe7\xbe\x8e\xe4\xb8\xbd\xe4\xba\xba\xe7\x94\x9f'.decode('utf-8') '美麗人生' >>> b'\xc3\xc0\xc0\xf6\xc8\xcb\xc9\xfa'.decode('gbk').encode('utf-8') b'\xe7\xbe\x8e\xe4\xb8\xbd\xe4\xba\xba\xe7\x94\x9f'
1
2
3
4
5
6
7
8
9
10
上述的 b’str’ 即為 bytecode,一個斜杠就是一個 byte。可見,一個常用漢字用 GBK 格式編碼后占 2byte,用 UTF-8 格式編碼后占 3byte。
在某些 Terminal 或 Console 中,String 的輸出總是出現亂碼,甚至錯誤,其實是由于 Terminal 或 Console 自身不能 decode 該 encode 類型的 string。
例如:
#-*-coding:utf-8-*- # 指定文件的 default coding(encode/decode)均為為 utf8 s1='中文' print type(s1) # 以 utf8 格式進行 str1 的編解碼 print s1 s2='中文' s2.encode('gb2312') # 強制將 utf8 str2 編碼為 gb2312 bytecode print type(s2) print s2
1
2
3
4
5
6
7
8
9
10
Output:
1
2
3
4
5
6
7
出現 UnicodeDecodeError 的原因是,當 print str 時,會隱式的調用 str() 進行 utf8 decode,如果 encode 和 decode 都是 utf8,那么可以正常輸出。否者,s.encode 為 gb2312 但卻以 utf8 decode 的話,就會出現 decode 異常。
Python 程序可以通過 #-*-coding:utf-8-*- 來指定文件的編碼格式,也可以全局修改系統默認的編碼類型:
import sys reload(sys) sys.setdefaultencoding('utf8')
1
2
3
4
Python
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。