Python decode()方法

2021-09-04 14:37 更新

Python decode()方法


描述

Python decode() 方法以 encoding 指定的編碼格式解碼字符串。默認(rèn)編碼為字符串編碼。

語法

decode() 方法語法:

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

參數(shù)

  • encoding -- 要使用的編碼,如"UTF-8"。
  • errors -- 設(shè)置不同錯(cuò)誤的處理方案。默認(rèn)為 'strict',意為編碼錯(cuò)誤引起一個(gè) UnicodeError。 其他可能得值有 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' 以及通過 codecs.register_error() 注冊(cè)的任何值。

返回值

該方法返回解碼后的字符串。

實(shí)例

以下實(shí)例展示了 decode() 方法的實(shí)例:

#!/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')

以上實(shí)例輸出結(jié)果如下:

Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=

Decoded String: this is string example....wow!!!
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)