zoukankan      html  css  js  c++  java
  • python2和python3的区别

    在python2和python3中,字符在内存中以unicode形式存在。在进行编码方式转换时,unicode形式应作为中介,在decode和encode中转换。如下图:

    python2中的字符编码

    在ipython中,默认编码为utf-8。

     1 In [1]: astr='你好,python'
     2 
     3 In [2]: astr
     4 Out[2]: 'xe4xbdxa0xe5xa5xbdxefxbcx8cpython'
     5 
     6 In [3]: astr.encode('utf8')
     7 ----------------------------------------------------
     8 UnicodeDecodeError                        Traceback
     9 <ipython-input-3-cb3c3e44ee4d> in <module>()
    10 ----> 1 astr.encode('utf8')
    11 
    12 UnicodeDecodeError: 'ascii' codec can't decode byte
    13 
    14 In [4]: astr.decode('utf8').encode('utf8')
    15 Out[4]: 'xe4xbdxa0xe5xa5xbdxefxbcx8cpython'
    16 
    17 In [5]: bstr=u'你好,python'
    18 
    19 In [6]: bstr
    20 Out[6]: u'u4f60u597duff0cpython'
    21 
    22 In [7]: bstr.encode('utf8')
    23 Out[7]: 'xe4xbdxa0xe5xa5xbdxefxbcx8cpython'

    在python shell中,默认编码为命令行终端的默认编码,作者的是windows7 cmd环境,默认编码为gb2312。

     1 >>> astr='你好,python'
     2 
     3 >>> astr
     4 'xc4xe3xbaxc3xa3xacpython'
     5 
     6 >>> astr.encode('utf8')
     7 Traceback (most recent call last):
     8   File "<stdin>", line 1, in <module>
     9 UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 0: ordinal not in range(128)
    10 
    11 >>> astr.decode('utf8').encode('utf8')
    12 Traceback (most recent call last):
    13   File "<stdin>", line 1, in <module>
    14   File "E:Anacondaenvspy27libencodingsutf_8.py", line 16, in decode
    15     return codecs.utf_8_decode(input, errors, True)
    16 UnicodeDecodeError: 'utf8' codec can't decode byte 0xc4 in position 0: invalid continuation byte
    17 
    18 >>> astr.decode('gb2312').encode('utf8')
    19 'xe4xbdxa0xe5xa5xbdxefxbcx8cpython'
    20 
    21 >>> bstr=u'你好,python'
    22 
    23 >>> bstr
    24 u'u4f60u597duff0cpython'
    25 
    26 >>> bstr.encode('utf8')
    27 'xe4xbdxa0xe5xa5xbdxefxbcx8cpython'

    在python2中,字符以各个终端环境中的默认编码或者指定的编码形式存储,进行编码转换时,需要先将字符解码转为unicode形式,在编码为指定形式的编码字符。

    python3中的字符编码

     在ipython中:

     1 In [1]: astr='你好,python'
     2 
     3 In [2]: astr
     4 Out[2]: '你好,python'
     5 
     6 In [3]: astr.encode('utf8')
     7 Out[3]: b'xe4xbdxa0xe5xa5xbdxefxbcx8cpython'
     8 
     9 In [4]: astr.decode('utf8').encode('utf8')
    10 ---------------------------------------------------------------------------
    11 AttributeError                            Traceback (most recent call last)
    12 <ipython-input-4-2a9e1e602dd8> in <module>()
    13 ----> 1 astr.decode('utf8').encode('utf8')

    在python3 shell中:

     1 >>> astr='你好,python'
     2 
     3 >>> astr
     4 '你好,python'
     5 
     6 >>> astr.encode('utf8')
     7 b'xe4xbdxa0xe5xa5xbdxefxbcx8cpython'
     8 
     9 >>> astr.decode('gb2312').encode('utf8')
    10 Traceback (most recent call last):
    11   File "<stdin>", line 1, in <module>
    12 AttributeError: 'str' object has no attribute 'decode'

    在python3中,字符默认是以unicode形式存储的。

    如果还有问题未能得到解决,搜索887934385交流群,进入后下载资料工具安装包等。最后,感谢观看!

  • 相关阅读:
    OCP-1Z0-052-V8.02-167题
    OCP-1Z0-052-V8.02-172题
    OCP-1Z0-052-V8.02-178题
    OCP-1Z0-052-V8.02-180题
    OCP-1Z0-052-V8.02-84题
    OCP-1Z0-052-V8.02-86题
    OCP-1Z0-052-V8.02-85题
    OCP-1Z0-052-V8.02-83题
    OCP-1Z0-052-V8.02-76题
    OCP-1Z0-052-V8.02-75题
  • 原文地址:https://www.cnblogs.com/pypypy/p/11836359.html
Copyright © 2011-2022 走看看