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!!!