1. 程式人生 > 實用技巧 >Python decode()方法

Python decode()方法

描述

Python decode() 方法以encoding指定的編碼格式解碼字串。預設編碼為字串編碼。高傭聯盟www.cgewang.com

語法

decode()方法語法:

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

引數

  • encoding -- 要使用的編碼,如"UTF-8"。

  • errors -- 設定不同錯誤的處理方案。預設為 'strict',意為編碼錯誤引起一個UnicodeError。 其他可能得值有 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' 以及通過 codecs.register_error() 註冊的任何值。

返回值

該方法返回解碼後的字串。

例項

以下例項展示了decode()方法的例項:

例項(Python 3.0+)

#!/usr/bin/python str = "this is string example....wow!!!"; str = str.encode('base64','strict'); print "Encoded String: " + str; print "Decoded String: " + str.decode('base64','strict')

以上例項輸出結果如下:

Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=

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