zoukankan      html  css  js  c++  java
  • python2和python3编码

    python2编码

              unicode:unicode 你好 u'u4f60u597d'
              | |                  | |
        encode('utf8')| |decode('utf8')      encode('gbk')| |decode('gbk')
              | |                  | |
              utf8                  gbk
    编码后的str 'xe4xbdxa0xe5xa5xbd'     编码后的gbk u'u6d63u72b2u30bd'


    # str: bytes

    >>> s = '你好 world'
    >>> print repr(s)
    'xe4xbdxa0xe5xa5xbd world'
    >>> print len(s)
    12
    >>> print type(s)
    <type 'str'>

    # unicode:unicode 

    >>> s = u'你好 world'
    >>> print repr(s)
    u'u4f60u597d 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'u4f60u597d'
              | |                  | |
        encode('utf8')| |decode('utf8')      encode('gbk')| |decode('gbk')
              | |                  | |
              utf8                  gbk
    编码后的str 'xe4xbdxa0xe5xa5xbd'     编码后的gbk u'u6d63u72b2u30bd'

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

    编码解码方式1:

    >>> s = '你好 world' 
    >>> b = s.encode('utf8')
    >>> print (b)
    b'xe4xbdxa0xe5xa5xbd 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'xc4xe3xbaxc3 world'
    >>> s = str(b,'gbk')
    >>> print (s)
    你好 world
    
    
    >>> s = '你好 world' 
    >>> b = bytes(s,'utf8') 
    >>> print (b)
    b'xe4xbdxa0xe5xa5xbd world'
    >>> s = str(b,'utf8')
    >>> print (s)
    你好 world
    >>> s = str(b,'gbk') 
    >>> print (s)
    浣犲ソ world
  • 相关阅读:
    [bzoj4417] [洛谷P3990] [Shoi2013] 超级跳马
    [bzoj4011] [洛谷P3244] [HNOI2015] 落忆枫音
    [bzoj1875] [洛谷P2151] [SDOI2009] HH去散步
    [bzoj4827] [洛谷P3723] [Hnoi2017] 礼物
    [bzoj2326] [洛谷P3216] [HNOI2011] 数学作业
    [bzoj3105] [cqoi2013] 新Nim游戏
    [YTU]_2353 ( 长方柱类【C++ 类定义】)
    [YTU]_2627 (职工工资统计)
    [YTU]_2769( 结构体--成绩统计)
    [YTU]_2577( 小数计算——结构体)
  • 原文地址:https://www.cnblogs.com/gaoyuanzhi/p/8306868.html
Copyright © 2011-2022 走看看