python2编码

          unicode:unicode 你好 u'\u4f60\u597d'
          | |                  | |
    encode('utf8')| |decode('utf8')      encode('gbk')| |decode('gbk')
          | |                  | |
          utf8                  gbk
编码后的str '\xe4\xbd\xa0\xe5\xa5\xbd'     编码后的gbk u'\u6d63\u72b2\u30bd'

# str: bytes

>>> s = '你好 world'
>>> print repr(s)
'\xe4\xbd\xa0\xe5\xa5\xbd world'
>>> print len(s)
12
>>> print type(s)
<type 'str'>

# unicode:unicode

>>> s = u'你好 world'
>>> print repr(s)
u'\u4f60\u597d world'
>>> print len(s)
8
>>> print type(s)
<type 'unicode'>

#unicode: 无论什么字符在Unicode都有一个对应。

python2的特点
1.在python2中print把字节转成了Unicode

2.python2中以默认已ASCII编码
[root@localhost ~]# cat python.py
#coding:utf8 # 告诉解释器以utf8编码
print '你好'

python3编码
在python3中默认以utf8编码

            str:unicode 你好 u'\u4f60\u597d'
          | |                  | |
    encode('utf8')| |decode('utf8')      encode('gbk')| |decode('gbk')
          | |                  | |
          utf8                  gbk
编码后的str '\xe4\xbd\xa0\xe5\xa5\xbd'     编码后的gbk u'\u6d63\u72b2\u30bd'

>>> s = '你好 world'
>>> print (json.dumps(s))
"\u4f60\u597d world"
>>> print (len(s))
8
>>> print (type(s))
<class 'str'>

编码解码方式1:

>>> s = '你好 world'
>>> b = s.encode('utf8')
>>> print (b)
b'\xe4\xbd\xa0\xe5\xa5\xbd world'
>>> s = b.decode('utf8')
>>> print (s)
你好 world
>>> s = b.decode('gbk')
>>> print (s)
浣犲ソ world

编码解码方式2:

>>> s = '你好 world'
>>> b = bytes(s,'gbk')
>>> print (b)
b'\xc4\xe3\xba\xc3 world'
>>> s = str(b,'gbk')
>>> print (s)
你好 world >>> s = '你好 world'
>>> b = bytes(s,'utf8')
>>> print (b)
b'\xe4\xbd\xa0\xe5\xa5\xbd world'
>>> s = str(b,'utf8')
>>> print (s)
你好 world
>>> s = str(b,'gbk')
>>> print (s)
浣犲ソ world

python2和python3编码的更多相关文章

  1. python2和python3编码问题

    欢迎加入python学习交流群 667279387 一.什么是编解码 1.什么是unicode 2.编码方式 二.python中的编解码 1.python2 (1).encode() 和 .decod ...

  2. 字符编码、python2和python3编码的区别

    目录 字符编码 文本编辑器存储信息的过程 python解释器解释python代码的流程 python解释器与文本编辑器的异同 不同编码格式存入与读取数据的过程 乱码的分析 python2和python ...

  3. Python2 和 Python3 编码问题

    基本存储单元 位(bit, b):二进制数中的一个数位,可以是0或者1,是计算机中数据的最小单位. 字节(Byte,B):计算机中数据的基本单位,每8位组成一个字节. 1B = 8b 各种信息在计算机 ...

  4. python2与python3编码

    #coding:utf8#一#1.在python2中,默认以ASCII编码chcp 936import sysprint sys.getdefaultencoding()# ascii#str:byt ...

  5. Python2和Python3编码的区别

    Python2 python2中有两种储存变量的形式,第一种:Unicode:第二种:按照coding头来的. 假设python2用utf8存储x='中文',当你print(x)的时候,终端接收gbk ...

  6. python2与python3编码问题

    python2: UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 33: 解决办法: 在报错的页面添加代码:  ...

  7. python2与python3编码(练习)

    #_author:来童星#date:2019/12/9import jsons='star'a=s.encode('utf8')print(s,type(s))# star <class 'st ...

  8. pickle 在python2 to python3 编码出现错误

    pickle.load(file) UnicodeDecodeError: 'ascii' codec can't decode byte 0xf5 in position 2: ordinal no ...

  9. Python全栈之路----Python2与Python3

    金角大王Alex  python 之路,致那些年,我们依然没搞明白的编码 python2与python3的区别 py2 str = bytes 为什么有bytes? 是因为要表示图片.视频等二进制格式 ...

随机推荐

  1. vue,一路走来(5)--微信登录

    微信登录 今天又是周末了,想着博客还没记录完成.是的,下面记录一下微信登录遇到的问题. 在我的项目中,个人中心是需要完成授权登录才可以访问的,首先在定义路由的时候就需要多添加一个自定义字段requir ...

  2. vue-如何输出Hello world

    首先引入vue.js文件 <script src="vue.js"></script> <!DOCTYPE html> <html> ...

  3. BZOJ3926 诸神眷顾的幻想乡

    传送门 树上SAM! 显然如果树上一条一条字符串放的话那么是n^2的w 但是 题目的性质非常吼啊! 20个叶子节点 我们就可以 把所有叶子结点拎出来当根 全部扔到一个SAM里 就吼啦 最后的答案是   ...

  4. vue 全局 js 方法

    1.新增 getCurrentDataType.js 文件 import cookieUtils from '@/config/cookieUtils' function getCurrentData ...

  5. Angular JS - 8 - SeaJS与前端模块化

    一.前端模块化 关于前端模块化,参考以下链接 : https://github.com/seajs/seajs/issues/547 http://www.cnblogs.com/huiguo/cat ...

  6. LDD3 第10章 中断处理

    各种硬件和处理器打交道的周期不同,并且总是比处理器慢.必须有一种可以让设备在产生某个事件时通知处理器----中断. 中断仅仅是一个信号,如果硬件需要,就可以发送这个信号.Linux处理中断方式和用户空 ...

  7. HDU 6040 Hints of sd0061 —— 2017 Multi-University Training 1

    Hints of sd0061 Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  8. MySQL操作数据库值mysql事务

    创建一个无参数的事务     注意要写START TRANSACTION或者是Begin;Mysql会默认直接执行一个单元 MYSQL默认是自动提交的,也就是你提交一个QUERY,它就直接执行!我们可 ...

  9. QTP笔记--检查点、ChildObjects方法

    一.自带检查点函数 '最后一个参数为timeout,单位是毫秒 Browser( "SAP NetWeaver Portal" ).Page( "SAP NetWeave ...

  10. delphi vlc 安装bug 处理编译错误"0" is an invalid value for the "DebugInformation" parameter of the "DCC"

    处理编译错误"0" is an invalid value for the "DebugInformation" parameter of the "DCC" [摘要:http://blog.csdn ...