1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > Python encode()函数详解 Python编码解码

Python encode()函数详解 Python编码解码

时间:2019-09-13 20:37:01

相关推荐

Python encode()函数详解 Python编码解码

「作者主页」:士别三日wyx

「作者简介」:CSDN top100、阿里云博客专家、华为云享专家、网络安全领域优质创作者

「推荐专栏」:小白零基础《Python入门到精通》

encode

1、常见编码格式2、返回的是Bytes类型3、错误处理方式4、解码

encode() 可以对字符串进行「编码」,常用来对「中文」字符串进行编码,以解决「乱码」问题。

语法

string.encode( encoding, errors )

参数

encodeing :(可选)指定编码,默认 UTF-8errors :(可选)设置错误处理方式,默认

返回值

返回编码后的「新字符串」

实例:对字符串进行GBK编码

str1 = 'hello'print(str1.encode('gbk'))

输出:

b'hello'

1、常见编码格式

ASCLL:美国早期制定的编码规范,只能表示128个字符。GB2312:中国在ASCLL基础上对中文进行扩展,可以表示6763个汉字符号。GBK:对GB2312的进一步扩展(K指扩),收录了21886个汉字符号。GB18030:对GBK再一次扩充,收录了70244个汉字符号。Unicode:字符集,包含了世界上目前所有的符号,全球的所有文字都可以解析,字符是定长的,统一为16位UTF-8:使用最广的一种Unicode的实现方式,每次8个位传输数据;体积太大,国内通常使用GBK。UTF-16:Unicode的另一种实现方式,每次传输16位数据

各个编码实例:

print('hello'.encode('gb2312'))print('hello'.encode('gbk'))print('hello'.encode('gb18030'))print('hello'.encode('utf8'))print('hello'.encode('utf16'))

输出:

b'hello'b'hello'b'hello'b'hello'b'\xff\xfeh\x00e\x00l\x00l\x00o\x00'

仔细观察返回结果就会发现,字符串前面都加了个b,接下来我们了解一下这是什么含义。

2、返回的是Bytes类型

encode() 编码后会返回一个「Bytes类型」的结果,而不是「str类型」

str1 = 'hello'print(type(str1))print(type(str1.encode()))

输出:

<class 'str'><class 'bytes'>

bytes 和 str 都属于字符串类型:

str 以Unicode code points形式存储(人认识的形式)bytes 以byte形式存储(机器认识的形式)

bytes 是一个「二进制」序列对象,定义时在字符串前面加上b(英文可以,中文需要先encode)

str1 = b'hello'print(type(str1))

输出:

<class 'bytes'>

3、错误处理方式

encode() 在编码时,经常会遇到「无法编码」的字符,这时就可以用 errors 设置适当的处理方式:

strict:失败时引发错误ignore:忽略无法编码的字符backslashreplace:用反斜杠替换无法编码的字符namereplace:用解释字符的文本替换无法编码的字符replace:用问号替换无法编码的字符xmlcharrefreplace:用xml字符替换字符

如果给「两个参数」,可以自动按顺序复制给参数;如果只给「一个参数」,需要用参数名指定。

print('hello'.encode('gbk', 'strict'))print('hello'.encode(errors='ignore'))print('hello'.encode(errors='backslashreplace'))print('hello'.encode(errors='namereplace'))print('hello'.encode(errors='replace'))print('hello'.encode(errors='xmlcharrefreplace'))

4、解码

decode() 会将「bytes类型」转成「str类型」,这意味着它只能解码bytes类型的字符串,解码str类型的字符串会报错 AttributeError: ‘str’ object has no attribute ‘decode’

bytes类型格式是b'xxx',如果只有str形式的字符串(比如'\xe5\xbc\xa0\xe4\xb8\x89'),可以在前面加上b,变成bytes类型,再进行解码

print(b'\xe5\xbc\xa0\xe4\xb8\x89'.decode())

输出:

张三

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。