The Encode and Decode in Python

简介: Do you really know the encode and decode in Python?The encode and decode in Python are used to convert between strings and bytes. That we all know that the string in the computer storage and communication in the network is in the form of **byte sequence**, not the unicode.

This article is also posted on my blog, feel free to check the latest revision: The Encode and Decode in Python

Do you really know the encode and decode in Python?

The encode and decode in Python are used to convert between strings and bytes. That we all know that the string in the computer storage and communication in the network is in the form of byte sequence, not the unicode.

Encode

So the encode is used to transform the string to the byte sequence. And when you call the encode function, you need to specify the encoding type, such as utf-8, gbk, gb2312, etc. And python will use the encoding type to transform every character in the string to the corresponding byte sequence.

s = "你好,世界"
encoded_s = s.encode('utf-8')
print(encoded_s)
# b'\xe4\xbd\xa0\xe5\xa5\xbd\xef\xbc\x8c\xe4\xb8\x96\xe7\x95\x8c'
# the b is the prefix of the byte sequence.

Decode

And the decode is the function of byte sequence. It transform the byte sequence to the string. And you should all use the same encoding type to transform the byte sequence to the string.

b = b'\xe4\xbd\xa0\xe5\xa5\xbd\xef\xbc\x8c\xe4\xb8\x96\xe7\x95\x8c'
decoded_b = b.decode('utf-8')
print(decoded_b)
# 你好,世界
目录
相关文章
|
10月前
|
存储 Python
Python中encode和encoding的区别
Python中encode和encoding的区别
142 0
|
自然语言处理 测试技术 Python
软件测试|深入理解Python的encode()和decode()方法
软件测试|深入理解Python的encode()和decode()方法
|
Python
Python3 ‘str‘ object has no attribute ‘decode‘. Did you mean: ‘encode‘?
Python3 ‘str‘ object has no attribute ‘decode‘. Did you mean: ‘encode‘?
319 0
|
编解码 测试技术 Python
Python UnicodeEncodeError 'ascii' codec can't encode character 错误解决方法
Python UnicodeEncodeError 'ascii' codec can't encode character 错误解决方法
130 0
|
机器学习/深度学习 移动开发 安全
Base64编码和Python解码
Base64编码和Python解码
408 0
Base64编码和Python解码
|
Python
python3 base编码解码
python3 base编码解码
88 1
|
存储 自然语言处理 JavaScript
python 的encode和decode
python 的encode和decode
python 的encode和decode
|
编解码 Python
【错误记录】PyCharm 运行 Python 程序报错 ( UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0xe5 in positio )
【错误记录】PyCharm 运行 Python 程序报错 ( UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0xe5 in positio )
392 0
【错误记录】PyCharm 运行 Python 程序报错 ( UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0xe5 in positio )
|
编解码 Python
Python 技术篇-含中文编码的代码运行方法,(unicode error) ‘utf-8‘ codec can‘t decode问题原因及解决方法
Python 技术篇-含中文编码的代码运行方法,(unicode error) ‘utf-8‘ codec can‘t decode问题原因及解决方法
735 0
Python 技术篇-含中文编码的代码运行方法,(unicode error) ‘utf-8‘ codec can‘t decode问题原因及解决方法
|
编解码 数据安全/隐私保护 Python
Python 技术篇 - 修改pyminifier库源码解决编码不一致导致的报错问题:‘gbk‘ codec can‘t decode byte 0x80 in position 54
Python 技术篇 - 修改pyminifier库源码解决编码不一致导致的报错问题:‘gbk‘ codec can‘t decode byte 0x80 in position 54
457 0
Python 技术篇 - 修改pyminifier库源码解决编码不一致导致的报错问题:‘gbk‘ codec can‘t decode byte 0x80 in position 54