项目马上就要由python2迁移到python3环境所有就简单总结下区别,个人觉得这些差不多,详情见如下吧
Python2 中使用 ASCII 码作为默认编码方式导致 string 有两种类型 str 和 unicode,Python3 只 支持 unicode 的 string。Python2 和 Python3 字节和字符对应关系为:
Python2 中相对路径的 import 会导致标准库导入变得困难(想象一下,同一目录下有 file.py,如 何同时导入这个文件和标准库 file)。
Python3 中这一点将被修改,如果还需要导入同一目录的文件必 须使用绝对路径,否则只能使用相关导入的方式来进行导入。
Python3统一采用新式类。新式类声明要求继承object, 必须用新式类应用多重继承
Python2 的缩进机制中,1 个 tab 和 8 个 space 是等价的,所 以在缩进中可以同时允许 tab 和 space 在代码中共存。这种等价机制会导致部分 IDE 使用存在问题。
Python3 中 1 个 tab 只能找另外一个 tab 替代,因此 tab 和 space 共存会导致报错:TabError: inconsistent use of tabs and spaces in indentation
mydict={"a":1,"b":2,"c":3}
mydict.keys() #
list(mydict.keys()) #['a', 'c', 'b']
Python2 | Python3 |
---|---|
urllib2.urlopen() | urllib.request.urlopen() |
urllib2.Request() | urllib.request.Request() |
htmllib.HTMLParser | html.parser.HTMLParse |
httplib | http.client |
python3将python2中的urllib2、urlparse、robotparser并入了urllib模块,并且修改了urllib模块,其中包含了5个子模块,每个子模块中的常用方法如下:
模块名 | 常用方法 |
---|---|
urllib.error | ContentTooShortError、URLError、HTTPError |
urllib.parse | urlparse、_splitparams、urlsplit、urlunparse、urlunsplit、urljoin、urldefrag、unquote_to_bytes、unquote、parse_qs、parse_qsl、unquote_plus、quote、quote_plus、quote_from_bytes、urlencode、to_bytes、unwrap、splittype、splithost、splituser、splitpasswd、splitport |
urllib.request | install_opener、urlretrieve、 urlcleanup、 request_host、 build_opener、 _parse_proxy、 parse_keqv_list、 parse_http_list、 _safe_gethostbyname、 ftperrors、 noheaders、 getproxies_environment、 proxy_bypass_environment、 _proxy_bypass_macosx_sysconf、 Request |
urllib.response | addbase、 addclosehook、 addinfo、 addinfourl |
urllib.robotparser | RobotFileParser |
“ / ”:
Python2:若为两个整形数进行运算,结果为整形,但若两个数中有一个为浮点数,则结果为 浮点数;
Python3:为真除法,运算结果不再根据参加运算的数的类型。
“//”:
Python2:
返回小于除法运算结果的最大整数;从类型上讲,与"/"运算符返回类型逻辑一致。
Python3:和 Python2 运算结果一样。
Python2
Python3
Python2,for 循环会修改外部相同名称变量的值
i = 1
print ('comprehension: ', [i for i in range(5)])
print ('after: i =', i ) #i=4
Python3,for 循环不会修改外部相同名称变量的值
i = 1
print ('comprehension: ', [i for i in range(5)])
print ('after: i =', i
Python2,round 函数返回 float 类型值
>>> isinstance(round(15.5),float)
True
>>> round(15.5)
16.0
Python3,round 函数返回 int 类型值
>>> isinstance(round(15.5),int)
True
>>> round(15.5)
16
Python2 中任意两个对象都可以比较
>>> 11 < 'test'
True
Python3 中只有同一数据类型的对象可以比较
>>> 11 < 'test'
File "", line 1
11 < 'test'
^
IndentationError: unexpected indent
Python2 无法安装 mysqlclient。
Python3 无法安装 MySQL-python、 flup、functools32、 Gooey、Pywin32、 webencodings。matplotlib 在 python3 环境中安装报错:The following required packages can not be built:freetype, png。需要手动下载安装源码包安装解决。 scipy 在 Python3 环境中安装报错,numpy.distutils.system_info.NotFoundError,需要自己手 工下载对应的安装包,依赖 numpy,pandas 必须严格根据 python 版本、操作系统、64 位与否。运行 matplotlib 后发现基础包 numpy+mkl 安装失败,需要自己下载,国内暂无下载源
Python2 无法安装 mysql-python 和 mysqlclient 包,报错:EnvironmentError: mysql_config not found,解决方案是安装 mysql-devel 包解决。使用 matplotlib 报错:no module named _tkinter, 安装 Tkinter、tk-devel、tc-devel 解决。 pywin32 也无法在 centos 环境下安装。
Python3与Python2有哪些区别?
和羞走,倚门回首,却把青梅嗅。 ------李清照