zoukankan      html  css  js  c++  java
  • Python中的encode和decode

    原文地址:http://www.cnblogs.com/tingyugetc/p/5727383.html

    1.Python3中对文本和二进制数据进行了比较清晰的区分,文本总是 unicode ,由 str 类型进行表示;二进制数据则由 bytes 类型进行表示。不会将 str 和 bytes 偷偷混合在一起,在python3中会明显发现不能将两者拼接在一起,也不能在bytes中查找字符,然而在实际操作中却需要将两者进行转换操作以便完成相应的需求,两者之间的转换关系如下表示:

    1 str -> bytes :encode编码
    2 bytes -> str : decode解码

    字符串通过编码转换为二进制数据,二进制数据通过解码转换为字符串

     1 >>> text = '我是文本'
     2 >>> text
     3 '我是文本'
     4 >>> print(text)
     5 我是文本
     6 >>> bytesText = text.encode()
     7 >>> bytesText
     8 b'xe6x88x91xe6x98xafxe6x96x87xe6x9cxac'
     9 >>> print(bytesText)
    10 b'xe6x88x91xe6x98xafxe6x96x87xe6x9cxac'
    11 >>> type(text)
    12 <class 'str'>
    13 >>> type(bytesText)
    14 <class 'bytes'>
    15 >>> textDecode = bytesText.decode()
    16 >>> textDecode
    17 '我是文本'
    18 >>> print(textDecode)
    19 我是文本

    2.其中 decode( ) 和 encode( ) 方法可以接受参数,其声明如下:

    1 bytes.decode(encoding="utf-8", errors="strict")
    2 str.encode(encoding="utf-8", errors="strict")

    其中 encoding 是指在编码过程中所使用的编码, errors 是指错误的处理方案。

  • 相关阅读:
    十万个为什么 —— 理化篇
    地行、地貌
    工业镜头基础知识整理
    parfor —— matlab 下的并行循环
    浅谈设计模式在GIS中的应用
    android ellipsize 属性详解
    Servlet 学习总结-1
    搜索引擎有用的外链建设的方式
    Servlet 学习总结-2
    如何用批处理命令批量配对重命名
  • 原文地址:https://www.cnblogs.com/danielStudy/p/6540962.html
Copyright © 2011-2022 走看看