Python 字串 decode() 方法



Python 字串的 decode() 方法使用為其編碼註冊的編解碼器解碼字串。可以使用此函式解碼編碼的字串並獲得原始字串。此函式的工作原理基於指定的引數,即編碼和錯誤。

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

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

語法

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

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

引數

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

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

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

返回值

Python 字串 decode() 方法返回解碼後的字串。

示例

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

在下面的示例中,建立一個字串“Hello! Welcome to Tutorialspoint.”,並使用 encode() 函式對其進行編碼。現在獲取編碼的字串,並對該字串呼叫 decode() 函式。獲得解碼後的字串作為輸出,並使用 print() 函式列印它。在這兩種情況下,使用的編碼都是 'utf_16',使用的錯誤是 'strict'。

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

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

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'
The decoded string is:  Hello! Welcome to Tutorialspoint.

示例

將 'euc_kr' 作為其編碼的 Python 字串 decode() 方法對韓文字元執行可變長度編碼。如果錯誤指定為 'strict',則編碼錯誤會引發 UnicodeError。

在下面的示例中,建立一個字串“Hello! Welcome to Tutorialspoint.”,並使用 encode() 函式對其進行編碼。現在獲取編碼的字串,並對該字串呼叫 decode() 函式,獲得解碼後的字串作為輸出,並使用 print() 函式列印它。在這兩種情況下,使用的編碼都是 'euc_kr',使用的錯誤是 'strict'。

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

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

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

示例

將 'utf_32' 作為其編碼的 Python 字串 decode() 方法執行可變長度編碼。如果錯誤指定為 'replace',則它將被替換字元替換。這是在 replace_errors() 中實現的。

在下面的例子中,建立了一個字串 "Hello! Welcome to Tutorialspoint.",並使用 `encode()` 函式對其進行編碼。然後,使用 `decode()` 函式對編碼後的字串進行解碼,並將解碼後的字串作為輸出打印出來。在這兩種情況下,使用的編碼都是 'utf_32',使用的錯誤處理方式是 'replace'。

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

執行上述程式後,得到以下輸出:

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'
The decoded string is:  Hello! Welcome to Tutorialspoint.

示例

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

在下面的例子中,建立了一個字串 "Hello! Welcome to Tutorialspoint.",並使用 `encode()` 函式對其進行編碼。然後,使用 `decode()` 函式對編碼後的字串進行解碼,並將解碼後的字串作為輸出打印出來。在這兩種情況下,使用的編碼都是 'utf_16_be',使用的錯誤處理方式是 'backslashreplace'。

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

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

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.'
The decoded string is:  Hello! Welcome to Tutorialspoint.

示例

Python 字串 `decode()` 函式在沒有引數的情況下,會使用預設的編碼值並相應地執行。

在下面的例子中,建立了一個字串 "Hello! Welcome to Tutorialspoint.",並使用沒有引數的 `encode()` 函式對其進行編碼。然後,使用沒有引數的 `decode()` 函式對編碼後的字串進行解碼,並將解碼後的字串作為輸出打印出來。

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

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

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