Python中字符串的encode()和decode()方法用于在文本(str)与二进制数据(bytes)间转换,encode()将字符串按指定编码(如utf-8)转为字节串,decode()将字节串还原为字符串,需确保编解码格式一致,否则会引发UnicodeEncodeError或UnicodeDecodeError,常见解决方案是统一使用UTF-8编码并合理处理错误参数。

Python中字符串的
encode()
decode()
encode()
decode()
理解
encode()
decode()
str
bytes
str.encode(encoding='utf-8', errors='strict')
这个方法是字符串对象调用的,它的作用是将一个字符串按照指定的
encoding
立即学习“Python免费学习笔记(深入)”;
encoding
'utf-8'
'gbk'
'latin-1'
errors
encoding
'strict'
UnicodeEncodeError
'ignore'
'replace'
?
\xbf
'xmlcharrefreplace'
{
'backslashreplace'
\xNN
\uNNNN
示例:
s = "你好,世界!Hello, World!"
# 使用UTF-8编码
b_utf8 = s.encode('utf-8')
print(f"UTF-8编码结果: {b_utf8}")
# 输出: UTF-8编码结果: b'\xe4\xbd\xa0\xe5\xa5\xbd\xef\xbc\x8c\xe4\xb8\x96\xe7\x95\x8c\xef\xbc\x81Hello, World!'
# 尝试使用GBK编码(如果字符串中包含GBK不支持的字符,可能会出错,但这里没问题)
b_gbk = s.encode('gbk')
print(f"GBK编码结果: {b_gbk}")
# 输出: GBK编码结果: b'\xc4\xe3\xba\xc3\xa3\xac\xca\xc0\xbd\xe7\xa3\xa1Hello, World!'
# 编码错误处理示例
s_euro = "Résumé" # 包含特殊字符 é
try:
s_euro.encode('ascii') # ASCII不支持 é
except UnicodeEncodeError as e:
print(f"编码错误(strict模式): {e}")
# 输出: 编码错误(strict模式): 'ascii' codec can't encode character '\xe9' in position 1: ordinal not in range(128)
b_replace = s_euro.encode('ascii', errors='replace')
print(f"替换模式编码: {b_replace}")
# 输出: 替换模式编码: b'R?sum?'bytes.decode(encoding='utf-8', errors='strict')
这个方法是字节串对象调用的,它的作用是将一个字节串按照指定的
encoding
encoding
encoding
UnicodeDecodeError
errors
encoding
'strict'
UnicodeDecodeError
'ignore'
'replace'
'xmlcharrefreplace'
'backslashreplace'
encode
示例:
# 承接上面的编码结果
b_utf8 = b'\xe4\xbd\xa0\xe5\xa5\xbd\xef\xbc\x8c\xe4\xb8\x96\xe7\x95\x8c\xef\xbc\x81Hello, World!'
b_gbk = b'\xc4\xe3\xba\xc3\xa3\xac\xca\xc0\xbd\xe7\xa3\xa1Hello, World!'
# 使用正确的UTF-8解码
s_decoded_utf8 = b_utf8.decode('utf-8')
print(f"UTF-8解码结果: {s_decoded_utf8}")
# 输出: UTF-8解码结果: 你好,世界!Hello, World!
# 使用正确的GBK解码
s_decoded_gbk = b_gbk.decode('gbk')
print(f"GBK解码结果: {s_decoded_gbk}")
# 输出: GBK解码结果: 你好,世界!Hello, World!
# 解码错误处理示例(故意用错编码)
try:
b_utf8.decode('gbk') # 尝试用GBK解码UTF-8字节串
except UnicodeDecodeError as e:
print(f"解码错误(strict模式): {e}")
# 输出: 解码错误(strict模式): 'gbk' codec can't decode byte 0xef in position 6: illegal multibyte sequence
s_decoded_replace = b_utf8.decode('gbk', errors='replace')
print(f"替换模式解码: {s_decoded_replace}")
# 输出: 替换模式解码: 你好,世界�Hello, World! (注意乱码部分被替换)总的来说,记住一个基本原则:编码和解码时使用的编码格式必须一致。如果编码时用了UTF-8,那么解码时也必须用UTF-8,否则就会出现乱码或者错误。这就像你用中文写了一封信,对方却用日文的规则去读,那肯定读不懂。
在Python中处理字符串编码和解码,说实话,是个老生常谈但又特别容易踩坑的问题。我个人在处理各种文件导入、网络数据传输时,没少因为编码问题抓狂。最常见的两个大坑就是
UnicodeEncodeError
UnicodeDecodeError
1. UnicodeEncodeError
这个错误通常发生在你尝试将一个字符串编码成字节串时,但字符串中包含了目标编码格式无法表示的字符。比如,你想把一个包含中文的字符串编码成
'ascii'
陷阱场景:
'latin-1'
解决方案:
明确指定编码: 永远不要依赖系统默认编码。在
encode()
'utf-8'
my_string = "你好,世界!"
try:
my_string.encode('ascii') # 会报错
except UnicodeEncodeError:
print("ASCII编码不支持中文!")
my_string_bytes = my_string.encode('utf-8') # 正确的做法
print(my_string_bytes)使用errors
errors='replace'
errors='ignore'
s = "Python编码测试,包含特殊字符é"
# 忽略无法编码的字符
b_ignore = s.encode('ascii', errors='ignore')
print(f"忽略后: {b_ignore}") # 输出: b'Python编码测试,包含特殊字符'
# 替换无法编码的字符
b_replace = s.encode('ascii', errors='replace')
print(f"替换后: {b_replace}") # 输出: b'Python\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\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x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f以上就是python中字符串的encode()和decode()怎么用?的详细内容,更多请关注php中文网其它相关文章!
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号