zoukankan      html  css  js  c++  java
  • Python3笔记023

    5.2 字符串编码转换

    5.2.1 encode()方法以 encoding 指定的编码格式编码字符串

    str.encode([encoding="utf-8"][errors="strict"])
    
    str1 = "星期一星期二星期三星期四星期五星期六星期日"
    byte1 = str1.encode('UTF-8')
    print(byte1)
    output:
    b'xe6x98x9fxe6x9cx9fxe4xb8x80xe6x98x9fxe6x9cx9fxe4xbax8cxe6x98x9fxe6x9cx9fxe4xb8x89xe6x98x9fxe6x9cx9fxe5x9bx9bxe6x98x9fxe6x9cx9fxe4xbax94xe6x98x9fxe6x9cx9fxe5x85xadxe6x98x9fxe6x9cx9fxe6x97xa5'
    

    5.2.2 decode()方法以 encoding 指定的编码格式解码字符串

    bytes.decode([encoding="utf-8"][,errors="strict"])
    
    byte1 = b'xe6x98x9fxe6x9cx9fxe4xb8x80xe6x98x9fxe6x9cx9fxe4xbax8cxe6x98x9fxe6x9cx9fxe4xb8x89xe6x98x9fxe6x9cx9fxe5x9bx9bxe6x98x9fxe6x9cx9fxe4xbax94xe6x98x9fxe6x9cx9fxe5x85xadxe6x98x9fxe6x9cx9fxe6x97xa5'
    str1 = byte1.decode('GBK')
    print(str1)
    output:
    星期一星期二星期三星期四星期五星期六星期日
    
  • 相关阅读:
    004 使用文本编辑器
    003 第一个Python程序
    002 Python解释器
    001 安装Python
    000 Python教程
    001 Java环境变量配置
    002 基础语法1
    003 基础语法2
    dede首页调用会员积分和头像代码
    DEDE 会员调用方法
  • 原文地址:https://www.cnblogs.com/infuture/p/13237589.html
Copyright © 2011-2022 走看看