#Python中字符串的编码与解码

29.字符串的编码与解码
编码(str).encode()
encode(encoding=“utf-8”,errors=“strict”)
encoding 默认 utf-8
errors 默认 strict
gbk为国标码
编码用的什么码 解码的时候就要用对应的码解

str51 = "AAAAA is a good good man"
data52 = str51.encode()
print(str51.encode())
#(输出)b'AAAAA is a good good man'

str54 = "AAAAA is a good good man 赵"
data55 = str54.encode()
print(str54.encode())
#(输出)b'AAAAA is a good good man \xe8\xb5\xb5'

解码(str).decode()
注意:要与编码是的编码格式一致

str51 = "AAAAA is a good good man"
data52 = str51.encode()
str54 = "AAAAA is a good good man 赵"
data55 = str54.encode()

str53 = data52.decode()
print(str53)
#(输出)AAAAA is a good good man

str56 = data55.decode()
print(str56)
#(输出)AAAAA is a good good man 赵

猜你喜欢

转载自blog.csdn.net/weixin_43097301/article/details/82954508