python中字符编码encode与解码decode的使用方法

encode 编码:str(或其他类型)—>byte

decode 解码:byte—>str(或其他类型)

数据在传输过程中都是以字节码的形式存在的

encode(encoding="",errors="") 和decode((encoding="",errors="") 中参数

其中:

  • encoding是代表编码格式常用例如:encoding="utf-8"或者enconding="gbk"。
  • errors是指错误的处理方案,例如:errors="strict"或者errors="ignore"。不写errors参数时默认errors="strict"
  1. strict:表示严格按照指定编解码方式进行编码和解码,如果编解码不成功则崩溃
  2. ignore:表示忽略编解码不成功的字符,如果编解码不成功程序不会崩溃

猜你喜欢

转载自blog.csdn.net/qq_34776336/article/details/81458619