Python 字串 encode() 方法



Python 字串的 encode() 方法使用為其編碼註冊的編解碼器對字串進行編碼。此函式基於指定的引數(編碼和錯誤)工作。

有各種型別的標準編碼,例如 base64、ascii、gbk、hz、iso2022_kr、utf_32、utf_16 等等。根據指定的編碼,字串被編碼。在此過程中,可以使用 errors 引數設定不同的錯誤處理方案。最後,此方法返回編碼後的字串。

在下一節中,我們將學習有關 Python 字串 encode() 方法的更多詳細資訊。

語法

Python 字串 encode() 方法的語法如下所示。

Str.encode(encoding='UTF-8',errors='strict')

引數

以下是 Python 字串 encode() 函式的引數。

  • encoding − 此引數指定要使用的編碼。有關所有編碼方案的列表,請訪問:標準編碼。

  • errors − 此引數指定錯誤處理方案。errors 的預設值為 'strict',這意味著編碼錯誤會引發 UnicodeError。其他可能的值為 'ignore'、'replace'、'xmlcharrefreplace'、'backslashreplace' 以及透過 codecs.register_error() 註冊的任何其他名稱。

返回值

Python 字串 encode() 方法返回一個編碼後的字串。

示例

將 'utf_16' 作為其編碼的 Python 字串 encode() 方法執行可變長度編碼。如果錯誤指定為 'strict',則編碼錯誤將引發 UnicodeError。

在以下示例中,建立了一個字串 "Hello! Welcome to Tutorialspoint.",並使用 encode() 函式對其進行編碼。編碼後的字串使用 print() 函式作為輸出列印。在這種情況下,使用的編碼是 'utf_16',使用的錯誤是 'strict'。

str="Hello! Welcome to Tutorialspoint."
str_encoded= str.encode('utf_16','strict')
print("The encoded string is: ", str_encoded)

執行上述程式後,將生成以下輸出 -

The encoded string is:  b'\xff\xfeH\x00e\x00l\x00l\x00o\x00!\x00 \x00W\x00e\x00l\x00c\x00o\x00m\x00e\x00 \x00t\x00o\x00 \x00T\x00u\x00t\x00o\x00r\x00i\x00a\x00l\x00s\x00p\x00o\x00i\x00n\x00t\x00.\x00'

示例

Python 字串的 encode() 方法,當使用 'euc_kr' 作為編碼時,會對韓文字元進行變長編碼。如果錯誤指定為 'strict',則編碼錯誤會引發 UnicodeError。

在以下示例中,建立了一個字串 "Hello! Welcome to Tutorialspoint.",並使用 encode() 函式對其進行編碼。使用 print() 函式列印編碼後的字串作為輸出。在這兩種情況下,使用的編碼都是 'euc_kr',使用的錯誤處理都是 'strict'。

str="Hello! Welcome to Tutorialspoint."
str_encoded= str.encode('euc_kr','strict')
print("The encoded string is: ", str_encoded)

執行上述程式後獲得的輸出如下:

The encoded string is:  b'Hello! Welcome to Tutorialspoint.'

示例

Python 字串的 encode() 方法,當使用 'utf_32' 作為編碼時,會進行變長編碼。如果錯誤指定為 'replace',則會用替換標記替換。這在 replace_errors() 中實現。

在以下示例中,建立了一個字串 "Hello! Welcome to Tutorialspoint.",並使用 encode() 函式對其進行編碼。使用 print() 函式列印編碼後的字串作為輸出。在這兩種情況下,使用的編碼都是 'utf_32',使用的錯誤處理都是 'replace'。

str="Hello! Welcome to Tutorialspoint."
str_encoded= str.encode('utf_32','replace')
print("The encoded string is: ", str_encoded)

執行上述程式後獲得的輸出如下:

The encoded string is:  b'\xff\xfe\x00\x00H\x00\x00\x00e\x00\x00\x00l\x00\x00\x00l\x00\x00\x00o\x00\x00\x00!\x00\x00\x00 \x00\x00\x00W\x00\x00\x00e\x00\x00\x00l\x00\x00\x00c\x00\x00\x00o\x00\x00\x00m\x00\x00\x00e\x00\x00\x00 \x00\x00\x00t\x00\x00\x00o\x00\x00\x00 \x00\x00\x00T\x00\x00\x00u\x00\x00\x00t\x00\x00\x00o\x00\x00\x00r\x00\x00\x00i\x00\x00\x00a\x00\x00\x00l\x00\x00\x00s\x00\x00\x00p\x00\x00\x00o\x00\x00\x00i\x00\x00\x00n\x00\x00\x00t\x00\x00\x00.\x00\x00\x00'

示例

Python 字串的 encode() 方法,當使用 'utf_32' 作為編碼時,會進行變長編碼。如果錯誤指定為 'backslashreplace',則會實現 'backslashreplace' 錯誤處理。

在以下示例中,建立了一個字串 "Hello! Welcome to Tutorialspoint.",並使用 encode() 函式對其進行編碼。使用 print() 函式列印編碼後的字串作為輸出。在這兩種情況下,使用的編碼都是 'utf_16_be',使用的錯誤處理都是 'backslashreplace'。

str="Hello! Welcome to Tutorialspoint."
str_encoded= str.encode('utf_16_be','backslashreplace')
print("The encoded string is: ", str_encoded)

執行上述程式後,顯示以下輸出:

The encoded string is:  b'\x00H\x00e\x00l\x00l\x00o\x00!\x00 \x00W\x00e\x00l\x00c\x00o\x00m\x00e\x00 \x00t\x00o\x00 \x00T\x00u\x00t\x00o\x00r\x00i\x00a\x00l\x00s\x00p\x00o\x00i\x00n\x00t\x00.'

示例

Python 字串的 encode() 函式,如果沒有傳遞任何引數,則會使用預設的編碼值並相應地執行。

在以下示例中,建立了一個字串 "Hello! Welcome to Tutorialspoint.",並使用 encode() 函式(不帶任何引數)對其進行編碼。使用 print() 函式列印編碼後的字串作為輸出。

str="Hello! Welcome to Tutorialspoint."
str_encoded= str.encode()
print("The encoded string is: ", str_encoded)

上述程式的輸出如下所示:

The encoded string is:  b'Hello! Welcome to Tutorialspoint.'
python_strings.htm
廣告