编程语言
首页 > 编程语言> > Python str.decode

Python str.decode

作者:互联网

String Decode方法


功能描述

decode方法对使用编码的字符串进行解码。

语法

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

参数

返回值

解码的字符串

示例

Str = "你好"
Str = Str.encode('utf-8','strict')
print("Encoded String: ",Str)
print("Decoded String: " + Str.decode('utf-8','strict'))

执行结果

Encoded String:  b'\xe4\xbd\xa0\xe5\xa5\xbd'
Decoded String: 你好

如果编码与解码方式不一样,且更改错误码后

print("Decoded String: " + Str.decode('gb2312','ignore'))
print("Decoded String: " + Str.decode('gb2312','replace'))
print("Decoded String: " + Str.decode('gb2312','backslashreplace'))
print("Decoded String: " + Str.decode('gb2312','xmlcharrefreplace'))

执行结果

Decoded String: 浣濂
Decoded String: 浣�濂�
Decoded String: 浣\xa0濂\xbd
TypeError: decoding with 'gb2312' codec failed (TypeError: don't know how to handle UnicodeDecodeError in error callback)

Error的处理机制

applestr 发布了4 篇原创文章 · 获赞 0 · 访问量 88 私信 关注

标签:Decoded,String,Python,gb2312,decode,str,Str,print
来源: https://blog.csdn.net/applestr/article/details/104193811