Python 中的字元編碼
簡介
Python 在資料科學和機器學習等技術的資料處理領域佔據了顯著的位置。在 21 世紀,處理海量資料對組織來說是一項極具挑戰性的任務,而資料科學和機器學習的發展使得資料訪問變得更加容易。
在 Python 語言中,使用 Unicode,並表示為 UTF(通用字元集傳輸格式)-8。對於字元的編碼,主要有兩種可能性:ASCII 字元和非 ASCII 字元。例如,我們取三個字元:
C = 67
D = 68
E = 69
D |
68 |
1000100 |
---|---|---|
字元 |
數字 |
二進位制數 |
字元編碼方法
有多種編碼技術可用,其中 UTF-8 是 Python 語言中使用的編碼技術。
UTF 使用一個位元組來表示 ASCII 字元。
另一方面,非 ASCII 字元根據其使用情況佔用兩個或更多位元組。
由於當前技術使用 Python 3,因此它在編碼過程中使用位元組和字串。
但在 Python 2 中,使用編碼將位元組陣列轉換為字串 'str'。
字元編碼是透過將給定的字元字串轉換為位元組來完成的,而解碼過程則涉及將位元組轉換為字串。
方法
方法 1 - 使用 ASCII 模組
方法 2 - 使用 hex() 方法
方法 1:使用 binascii 模組進行字元編碼的 Python 程式
使用 binascii() 方法,在“data”上呼叫 encode() 方法並使用“UTF-8”編碼技術將給定的字串轉換為編碼的二進位制格式。
演算法
步驟 1 - 匯入 binascii 模組以使用 binascii.hexlify() 函式。
步驟 2 - 使用字串元素初始化變數。
步驟 3 - 使用 type() 函式獲取已定義變數的資料型別,在本例中為字串變數。
步驟 4 - 然後將結果程式碼儲存在變數“encoding”中。
步驟 5 - print 語句將返回字串的型別和編碼資料。
示例
#importing the binascii module import binascii #initializing the data with a string of elements data = "Welcome to Tutorialpoint" #returns the type of data initialized print(type(data)) #convert the string into its equivalent binary format encoding = binascii.hexlify(data.encode('utf-8')) #returns the data after encoding the data print("Data after encoding is:", encoding)
輸出
<class 'str'> Data after encoding is: b'57656c636f6d6520746f205475746f7269616c706f696e74'
方法 2:使用 hex() 方法進行字元編碼的 Python 程式
使用 hex() 方法,在“data”上呼叫 encode() 方法並使用“UTF-8”編碼技術將給定的字串轉換為編碼的十六進位制格式。
演算法
步驟 1 - 使用字串元素初始化變數。
步驟 2 - 使用 type() 函式獲取已定義變數的資料型別,在本例中為字串變數。
步驟 3 - 然後將結果程式碼分配給變數“encoding”。
步驟 4 - 最後,顯示 encoding 變數的值。
示例
#initializing the data with a string of elements data = "Welcome to Tutorialpoint" #returns the type of data initialized print(type(data)) #convert the string into its equivalent hexadecimal format encoding = data.encode('utf-8').hex() #returns the data after encoding the data print("Data after encoding is:", encoding)
輸出
<class 'str'> Data after encoding is: 57656c636f6d6520746f205475746f7269616c706f696e74
結論
作為程式設計師,應在 Python 中對字元進行編碼以確保安全。字串的字元被編碼,用於系統和其他電子裝置。當資料從一個源傳輸到另一個源時,需要對資料或字元進行編碼。字串由 Unicode 物件組成,用於編碼,可用於全球範圍內。當 Unicode 被設計並可以在不同的作業系統和應用程式平臺上訪問時。