Python字串decode()方法

Python字串decode()方法使用註冊編碼(codec)的編解碼器解碼字串。它默認是默認字串編碼。

語法

以下是decode()方法的語法 -

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

參數

  • encoding - 這是要使用的編碼。有關所有編碼方案的列表,請訪問:標準編碼
  • errors - 可以給出這一點來設置不同的錯誤處理方案。錯誤的默認值為“strict”,這意味著編碼錯誤會引發UnicodeError。其他可能的值是“ignore”,“replace”,“xmlcharrefreplace”,“backslashreplace”以及通過codecs.register_error()註冊的任何其他名稱。

返回值

  • 此方法返回解碼的字串。

示例

以下示例顯示了decode()方法的用法。

#!/usr/bin/python3

str = "this is string example....wow!!!";
str = str.encode('base64','strict');

print "Encoded String: " + str
print "Decoded String: " + str.decode('base64','strict')

當運行上面的程式,它產生以下結果 -

Encoded String: b'dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE='

Decoded String: this is string example....wow!!!

上一篇: Python字串 下一篇: Python列表